1

I have a String that contains an xml like this:

<root>
   <type>
      <element>
         <thing>
            <otherthing>...</otherthing>
         </thing>
      </element>
   </type>
   <type>
      <element>
         <thing>
            <otherthing>...</otherthing>
         </thing>
      </element>
   </type>
   <type>
      <element>
         <thing>
            <otherthing>...</otherthing>
         </thing>
      </element>
   </type>
</root>

I need a treenode in my treeview for each indentation so I can expand and contract it when I want cause there is so much information in each node, how can I do it?

The result should be like this:

ROOT
---+type
--------+element
----------------+thing
----------------------+otherthing
---+type
--------+element
----------------+thing
----------------------+otherthing
---+type
--------+element
----------------+thing
----------------------+otherthing

Thank you!

1 Answer 1

4

Use a xml parser to parse the data, create a TreeItem representing it and use a TreeView to display the data.

Example:

private static class TreeItemCreationContentHandler extends DefaultHandler {

    private TreeItem<String> item = new TreeItem<>();

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        // finish this node by going back to the parent
        this.item = this.item.getParent();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        // start a new node and use it as the current item
        TreeItem<String> item = new TreeItem<>(qName);
        this.item.getChildren().add(item);
        this.item = item;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String s = String.valueOf(ch, start, length).trim();
        if (!s.isEmpty()) {
            // add text content as new child
            this.item.getChildren().add(new TreeItem<>(s));
        }
    }

}

public static TreeItem<String> readData(File file) throws SAXException, ParserConfigurationException, IOException {
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    SAXParser parser = parserFactory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    TreeItemCreationContentHandler contentHandler = new TreeItemCreationContentHandler();

    // parse file using the content handler to create a TreeItem representation
    reader.setContentHandler(contentHandler);
    reader.parse(file.toURI().toString());

    // use first child as root (the TreeItem initially created does not contain data from the file)
    TreeItem<String> item = contentHandler.item.getChildren().get(0);
    contentHandler.item.getChildren().clear();
    return item;
}
// display data for file "data/tree.xml" in TreeView
TreeItem<String> root = readData(new File("data/tree.xml"));
TreeView<String> treeView = new TreeView<>(root);
Sign up to request clarification or add additional context in comments.

5 Comments

You're amazing!! It works like a charm and saved me a lot of time!! Thank you very much!!
How can I limit the number of items I wanna display in my treeview?
Not sure what exactly you mean by that... If you only want to display N items, you could make sure there are only N-1 decendants of your root item or you could use a listener that automatically collapses some of the TreeItems, if too many items are expanded...
Imagine I have 10000 items but I only wanna show 1 in my treeview
And why would you use a TreeView for this purpose and not just a Label? The whole expanding/collapsing thing doesn't make sense if you just want to show a single item... But maybe you should ask a new question. This is most likely isn't answerable in the comments and maybe it could be useful to other users too. (And if it's not directly related to the XML parsing, try to leave that part out and simply create the tree from code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.