Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between XMLStreamReader and XMLEventReader?

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?

like image 689
Arunselvan Avatar asked Apr 19 '16 11:04

Arunselvan


People also ask

What is XMLEventReader?

public interface XMLEventReader extends Iterator. This is the top level interface for parsing XML Events. It provides the ability to peek at the next event and returns configuration information through the property interface.

What is XMLStreamReader?

The XMLStreamReader interface allows forward, read-only access to XML. It is designed to be the lowest level and most efficient way to read XML data. The XMLStreamReader is designed to iterate over XML using next() and hasNext().

How to read XML File in java using StAX parser?

XMLEventReader reads an XML file as a stream of events. It also provides the methods necessary to parse the XML. The most important methods are: isStartElement(): checks if the current event is a StartElement (start tag) isEndElement(): checks if the current event is an EndElement (end tag)


1 Answers

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.

like image 174
Grigory Kislin Avatar answered Oct 20 '22 07:10

Grigory Kislin