Java and XML tutorial with StAX parser – reading

Q. What is a StAX Parser and when will you use it?
A. The StAX Java API for XML processing is designed for parsing XML streams, just like the SAX API’s, but

  • StAX is a “pull” API whereas SAX is a “push” API.
  • StAX can do both XML reading and writing whereas SAX can only do XML reading.
Q. Why use StAX when there is DOM?
A. The DOM parsing involves creating in-memory objects representing an entire document tree for an XML document. Once in memory, DOM trees can be navigated and parsed arbitrarily, providing the maximum flexibility for developers. However this flexibility comes at a cost of large memory footprint and significant processor requirements, as the entire representation of the document must be held in memory as objects for the duration of the document processing. This may not be an issue when working with small documents, but memory and processor requirements can escalate quickly for larger size documents.

StAX (Streaming Api for XML) involves streaming where streaming refers to a programming model where XML data sets are transmitted and parsed serially at application runtime as events like StartElementEvent, CharacterEvent, EndElementEvent, etc, often in real time and dynamically where contents are not precisely known beforehand. These stream-based parsers can start generating output immediately, and data set elements can be discarded and garbage collected immediately after they are used. This ensures smaller memory footprint, reduced processor requirements, and higher performance in certain scenarios. This memory and processing efficiency comes at the cost where streaming can only see the  data set state at one location at a time in the document.


Here is how the events are sequentially processed:

Q. What is the major advantage of StAX over SAX?
A. The major advantage of StAX over SAX is that the pull model allows sub parsing of the XML input. You can extract out the element name, then the attributes, and then the characters (i.e. content)

Here is a simple code example to read the Employee from XML:

Output


300+ Java Interview FAQs

Tutorials on Java & Big Data