What is the difference between XMLStreamReader and XMLEventReader?

I am browsing the web. I found that XMLStreamReader is a cursor API for parsing XML . And XMLEventReader is an Iterator API for parsing XML . Can someone tell me in detail?

+9
source share
2 answers

See the explanation: https://www.ibm.com/developerworks/library/x-stax1/

Both XMLStreamReader and XMLEventReader allow the application to iterate through the main XML stream on its own. The difference between the two approaches is how they provide fragments of the parsed XML InfoSet. The XMLStreamReader acts like a cursor that points a little further than the most recently parsed XML token and provides methods for getting more information about it. This approach uses memory very efficiently because it does not create any new objects. However, business application developers may find XMLEventReader somewhat more intuitive, since it is in fact a standard Java Iterator that turns XML into a stream of event objects. Each event object in turn encapsulates information related to the specific XML structure that it represents. The second part of this series will give a detailed description of the API based on event iterators. Which API style to use depends on the situation. The Event Iterator API is a more object oriented approach than the cursor API. Thus, it is easier to apply in modular architectures, since the current state of the analyzer is reflected in the event object; thus, the application component does not need access to the analyzer / reader when processing the event. Alternatively, you can create an XMLEventReader from an XMLStreamReader using the XMLInputFactory createXMLEventReader (XMLStreamReader) method.

+6
source

I think the difference is that the streaming reader actually represents events.

XMLEvent event = eventReader.nextEvent(); if(event.getEventType() == XMLStreamConstants.START_ELEMENT){ StartElement startElement = event.asStartElement(); System.out.println(startElement.getName().getLocalPart()); } 

vs

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

Thus, each time an additional event object is created for reading events. Overhead can be significant since there are many, many events.

0
source

All Articles