I need to build a tree view of the elements present in an XML file. For example,
<abc>
<abcd/>
<abcd/>
</abc>
<def>
<defg/>
<jkl/>
<defg/>
</def>
My TreeView should look like this:
>abc
>abcd
>abcd
>def
>defg
>jkl
>defg
>def
I need to read the elements and build a treeView in JavaFX. I'm not sure which data structure to use here. If I use List> to store <(node, children)>, each child can have multiple children and they can have many and so on. So, I'm not able to figure out what data structure to use. Can anyone suggest what is the possible way here?
UPDATE: Using the reference in this Java: How to display an XML file in a JTree , I tried doing like this in JavaFX.
public TreeView<String> build(String pathToXml) throws Exception {
SAXReader reader = new SAXReader();
Document doc = (Document) reader.read(pathToXml);
return new TreeView<String>(build(doc.getRootElement()));
}
public TreeItem<String> build(Element e) {
TreeItem<String> item = new TreeItem<String>(e.getText());
for(Object o : e.elements()) {
Element child = (Element) o;
item.getChildren().add(build(child));
}
return item;
}
But I'm not able to see the content in TreeView. When I debugged, e.getText() is having only "\n\n". Is there a way to handle it? Am I doing this correctly?