I want to serialise my DefaultTreeModel to XML via JXB and have written a simple application which works when the object being marshalled is a LinkedList but when I convert it to a DefaultTreeModel, all I get in the output is <treeModel/>. Have used the debugger to make sure the tree is populated just before the XML is written. Does anyone have a clue why this doesn't work?
@XmlRootElement
public class Zoo {
private DefaultTreeModel treeModel;
public Zoo() {
DefaultMutableTreeNode node = new DefaultMutableTreeNode("test node");
this.treeModel = new DefaultTreeModel(node);
}
public void add( Animal a ) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(a);
this.treeModel.insertNodeInto(node, (DefaultMutableTreeNode) this.treeModel.getRoot(), ((DefaultMutableTreeNode) this.treeModel.getRoot()).getChildCount());
}
@XmlElement
public DefaultTreeModel getTreeModel() {
return this.treeModel;
}
public void setTreeModel(DefaultTreeModel treeModel) {
this.treeModel = treeModel;
}
}
public class JXBTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Zoo z = new Zoo();
Cat c = new Cat();
c.setName("Pussy");
c.setColour("Blue");
z.add( c );
c = new Cat();
c.setName("Meow");
c.setColour("Green");
z.add( c );
Dog d = new Dog();
d.setPaws(4);
z.add(d);
File f = new File("/tmp/animals.xml");
try {
JAXBContext context = JAXBContext.newInstance( Zoo.class );
Marshaller m = context.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal( z, f );
} catch( Exception ex ) {
ex.printStackTrace();
}
}
}
The output was:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo>
<treeModel/>
</zoo>
No comments:
Post a Comment