I want to do exactly what was asked here : Convert JTree to XML And the answer he got was nice BUT when I try do use it I got a INVALID_CHARACTER_ERR.
Here is an example of a XML file I have put in a JTree :
<?xml version="1.0"?>
<company0>
<staff id="1001">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company0>
And I'd like to make a function that returns a string (or directly a XML file) that returns the contents of the JTree.
How could I do that ?
your 'createTree' should probably look like this:
private static Element createTree(Document doc, TreeModel model, Object node) {
Element el = doc.createElement(node.toString());
for(int i=0;i<model.getChildCount(node);i++){
DefaultMutableTreeNode child = (DefaultMutableTreeNode)model.getChild(node, i);
if (child.isLeaf()) {
el.setTextContent(child.toString());
} else {
el.appendChild(createTree(doc,model,child));
}
}
return el;
}
Because the original createTree
converts node values to elements as there was a check (in the if condition above) missing