Search code examples
javaxmlstax

How to get XML element path using stax/stax2?


I want to get element path while parsing XML using java StAX2 parser. How to get information about the current element path?

<root>
  <a><b>x</b></a>
</root>

In this example the path is /root/a/b.


Solution

  • Keep a stack. Push the element name on START_ELEMENT and pop it on END_ELEMENT.

    Here's a short example. It does nothing other than print the path of the element being processed.

    public static void main(String[] args) throws IOException, XMLStreamException {
        try (FileInputStream in = new FileInputStream("test.xml")) {
    
            XMLInputFactory factory = XMLInputFactory.newFactory();
            XMLStreamReader reader = factory.createXMLStreamReader(in);
    
            LinkedList<String> path = new LinkedList<>();
    
            int next;
            while ((next = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
                switch (next) {
                    case XMLStreamConstants.START_ELEMENT:
                        // push the name of the current element onto the stack
                        path.addLast(reader.getLocalName());
                        // print the path with '/' delimiters
                        System.out.println("Reading /" + String.join("/", path));
                        break;
    
                    case XMLStreamConstants.END_ELEMENT:
                        // pop the name of the element being closed
                        path.removeLast();
                        break;
                }
            }
        }
    }