0

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?

Community
  • 1
  • 1
NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42
  • 1
    JDK has what you are looking for javax.swing.tree.TreeModel and TreeNode suffice for most use-cases. Do not re-invent the wheel. – ring bearer Jun 12 '15 at 09:27
  • can you post any link where XML is read and popluated into a tree? – NaveenBharadwaj Jun 12 '15 at 09:31
  • Research on xml file parsers in Java. Some of them provide a full traversable data model. Then you can put it into JavaFX TreeView. – Uluk Biy Jun 12 '15 at 09:45
  • You are in the boundary of being duplicate see this example : http://stackoverflow.com/questions/2011775/java-how-to-display-an-xml-file-in-a-jtree – ring bearer Jun 12 '15 at 09:50
  • @UlukBiy, I searched many examples. Everybody are doing getElementByTag(). In my case, I wouldn't know the tag. it can vary every single time. I couldn't find an example where they are recursively finding the elements representing parent-child relationship. – NaveenBharadwaj Jun 12 '15 at 09:58
  • @ringbearer, I'm using JavaFX and not swing. I dont see any way to convert JTree to JavaFX's TreeView – NaveenBharadwaj Jun 12 '15 at 10:00
  • @NaveenBharadwaj the http://stackoverflow.com/questions/2011775/java-how-to-display-an-xml-file-in-a-jtree can be easily converted to JavaFX version. Try it out, and if you will have difficulties update your question. – Uluk Biy Jun 12 '15 at 10:07
  • @UlukBiy, I tried converting the code to JavaFX. I have updated the question. Please correct me. – NaveenBharadwaj Jun 12 '15 at 10:23
  • Uh. Replaced e.getText() with e.getName() and it works. Thanks UlukBiy and ring bear :) – NaveenBharadwaj Jun 12 '15 at 10:26

0 Answers0