Search code examples
javaxmlstax

How to remember XMLStreamReader position in xml with STAX


I'm parsing big document with STAX. How do i remember, or clone, or save XMLStreamReader, or its position so i can return to that position later and continue reading as usual?

EDIT

I know theres getLocation() method but how do i assign Location to another reader?


Solution

  • You cannot do that. It is not possible to jump to certain location. StAX and as well SAX are forward only parsers. That kind of gives possibility to have efficient implementation.

    Via deep clone this is in theory possible, if your implementation supports it (unlikely). It sounds more that you need Dom or TrAX parser.

    If that is not possible because of size of document, then you have to modify your parsing so that is not needed to return to earlier point. Or you can memorize location in your code (for example based to the element count) and roll new parser to that point before actual processing.