Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

convert xml file to node tree with java

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;
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to use java code to print with a network printer 
Java :: java circular buffer implementation on array 
Java :: java hashset api 
Java :: Recyclerview scramble after scrolling 
Java :: java swing place objects vertically 
Java :: Uri/beecrowd problem no 1118 solution in Java 
Java :: Java Insert Elements 
Java :: contoh object dalam oop java 
Java :: java try with multiple resources 
Java :: spring component 
Java :: executors java 
Java :: java 8 seconds to days 
Java :: java digit in number 
Java :: java anonymous function 
Java :: java listview 
Java :: calculate number of weeks between two dates 
Java :: convert python code to java 
Java :: create thread 
Java :: error: incompatible types: NonExistentClass cannot be converted to Annotation 
Java :: postfix operator in java 
Java :: client missing intents 
Java :: selenium firefox to foreground -python java 
Sql :: sql server search column name in all tables 
Sql :: ADD COLOUNS CREATED AND UPDATED AT IN MYSQL 
Sql :: sql ignore accents 
Sql :: sql server find columns list in tables 
Sql :: oracle enable job 
Sql :: mysql group by month 
Sql :: string to date postgres 
Sql :: sql add date hour 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =