What is the difference between XMLStreamReader and XMLEventReader?

Arunselvan picture Arunselvan · Apr 19, 2016 · Viewed 7.3k times · Source

I surf through the web. I found that the XMLStreamReader is Cursor style API for parsing XML. And XMLEventReader is Iterator style API for Parsing XML.Could any one tell me in detail?

Answer

Grigory Kislin picture Grigory Kislin · Sep 23, 2016

Have a look at explanation: https://www.ibm.com/developerworks/library/x-stax1/

Both XMLStreamReader and XMLEventReader allow the application to iterate over the underlying XML stream on its own. The difference between the two approaches lies in how they expose pieces of the parsed XML InfoSet. The XMLStreamReader acts as a cursor that points just beyond the most recently parsed XML token and provides methods for obtaining more information about it. This approach is very memory-efficient as it does not create any new objects. However, business application developers might find XMLEventReader slightly more intuitive because it is actually a standard Java Iterator that turns the XML into a stream of event objects. Each event object in turn encapsulates information pertaining to the particular XML structure it represents. Part 2 of this series will provide a detailed description of the event iterator-based API. As to which API style to use depends on the situation. The event iterator-based API represents a more object-oriented approach than the cursor-based API. As such, it is easier to apply in modular architectures, because the current parser state is reflected in the event object; thus, an application component does not need access to the parser/reader while processing the event. Furthermore, it is possible to create an XMLEventReader from an XMLStreamReader using XMLInputFactory's createXMLEventReader(XMLStreamReader) method.