This articles discusses about Java Streaming API for XML (StAX) and its variations, Streaming and Cursor api.


StAX api

Streaming API for XML (StAX) is an application programming interface (API) to read and write XML documents.

StAX was created to address limitations in DOM and SAX api.


Difference between StAX and DOM API

DOM creates in-memory model for the entire document tree. For larger xmls, this can be memory and processor intensive as the entire representation of the document must be held in memory for the duration of the document processing.

SAX is a streaming api like SAX and has smaller memory and processor requirements.

Difference between StAX and SAX API


Both are streaming apis. While SAX is a push API, StAX is pull api.

StAX can both read and write XML documents. SAX can only read XML documents.


“Pull” vs. “Push” API


SAX is a push style API.

This means that the SAX parser iterates through the XML and calls methods on the handler object provided by you.

For instance, when the SAX parser encounters the beginning of an XML element, it calls the startElement on your handler object. It “pushes” the information from the XML into your object. Hence the name “push” style API.

Similarly, your handler object is notified with event-calls for elements, text etc.

StAX is a pull style API.

This means that you have to move the StAX parser from item to item in the XML file yourself, just like you do with a standard Iterator or JDBC ResultSet.

You can then access the XML information via the StAX parser for each item encountered in the XML file.

The StAX API is really two distinct API sets: a cursor API and an iterator API.


StAX Cursor API

StAX cursor API represents a cursor with which you can walk an XML document from beginning to end. This cursor can point to one thing at a time, and always moves forward, never backward, usually one infoset element at a time.

The two main cursor interfaces are XMLStreamReader and XMLStreamWriter. XMLStreamReader includes accessor methods for all possible information retrievable from the XML Information model, including document encoding, element names, attributes, namespaces, text nodes, start tags, comments, processing instructions, document boundaries, and so forth;


XMLStreamReader includes accessor methods for parsing XML files and retrieving information into java object.
convert xml to java object

public interface XMLStreamReader {
  public int next() throws XMLStreamException;
  public boolean hasNext() throws XMLStreamException;
  public String getText();
  public String getLocalName();
  public String getNamespaceURI();
  // ... other methods not shown


Here is an article that explains converting an XML document to Java object using StAX Cursor api:

Convert XML document to Java Object using XMLStreamReader



XMLStreamWriter provides methods for writing information from Java object into an xml.
convert java object to xml

public interface XMLStreamWriter {
  public void writeStartElement(String localName) 
    throws XMLStreamException;
  public void writeEndElement() 
    throws XMLStreamException;
  public void writeCharacters(String text) 
    throws   XMLStreamException;
// ... other methods not shown

Here is an article that explains converting a Java Object to XML document using StAX Cursor api:

Convert Java Object to XML using XMLStreamWriter

StAX Iterator API

The StAX iterator API represents an XML document stream as a set of discrete event objects. These events are pulled by the application and provided by the parser in the order in which they are read in the source XML document.



public interface XMLEventReader extends Iterator {
  public XMLEvent nextEvent() throws XMLStreamException;
  public boolean hasNext();
  public XMLEvent peek() throws XMLStreamException;




public interface XMLEventWriter {
  public void flush() throws XMLStreamException;
  public void close() throws XMLStreamException;
  public void add(XMLEvent e) throws XMLStreamException;
  public void add(Attribute attribute) \
    throws XMLStreamException;


StAX Cursor vs Iterator api

Cursor api generally provides better performance and has comparatively lower memory footprint.

Iterator api is more flexible and extensible. So, if the need is to modify the event stream or handle pluggable processing of event stream, Iterator api is preferable.

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

Leave a Reply.. code can be added in <code> </code> tags