Search code examples
javaxmlxmlstreamreader

XMLStreamReader - What happens at the end of the file?


When traversing an XML document like so

while(streamReader.hasNext()){
    streamReader.next();
    if(streamReader.getEventType() == XMLStreamReader.START_ELEMENT){
        System.out.println(streamReader.getLocalName());
    }
}

Do I need to create a new streamReader if I need to traverse the XML document again, like so?

   XMLStreamReader streamReader =
        factory.createXMLStreamReader(reader);

I don't see a method like 'reset()' to move the cursor back to the start of the XML file


Solution

  • Yes, you should create a new reader at that point.

    If you need to traverse the document multiple times, do you definitely want to parse it in a streaming fashion in the first place, rather than loading it into a DOM of some description?