Skip to content
Related Articles
Open in App
Not now

Related Articles

StAX vs SAX Parser in Java

Improve Article
Save Article
  • Last Updated : 14 Feb, 2022
Improve Article
Save Article

Streaming the API for XML, called the StAX, is an API for reading and writing the XML Documents. It was introduced in Java 6 and is considered superior to SAX and DOM which are other methods in Java to access XML. Java provides several ways [APIs] to access XML. Traditionally, XML APIs are either-

  • Tree-based [such as StAX, DOM]: The entire document is read into memory as the tree structure for random access by to the calling application
  • Event-based [such as SAX]: The application registers to receive events as these entities are encountered within the source document

StAX is a median between these two opposites. The StAX was created to address limitations in the two most prevalent parsing APIs, First SAX and Second DOM. In StAX, the programmatic entry point is a cursor that is, represents a point within the document. The application moves the cursor forward for pulling the information from the parser to as it needs. This is very different from an event-based API such as SAX where data is pushed to the application. This thus requires the application to maintain state between events as necessary to keep track of location within the document

StAX can do both the XML reading and writing whereas SAX can only do the XML reading. StAX is a Pull-Parsing model for XML which avoids most of the pitfalls. The application can take control over parsing the XML documents by pulling the events from the parser. Like SAX StAX 1s a parser-independent, pure Java API based on interfaces that can be implemented by multiple parsers. The main Primary Goal of the StAX API is to give the parsing control to the programmer by exposing a simple iterator based API. This allows the programmer to ask for the next event i.e.[pull the event] and allowing the state to be stored in the procedural fashion


SAX is a push APL. The SAX parser iterates through the XML and calls methods on the handler object provided by the developer. When the SAX parser encounters the beginning of an XML element, it calls start Element on the handler object. It pushes the information from the XML into the developer’s object, hence the name push API

SAX is also referred to as an event-driven API. This is because the handler object is notified with event calls when elements, texts, comments, and so on are found in the XML document DOM allows for random access to the document, whereas SAX requires a small memory footprint and is typically much faster. Both of these ways are opposite to each other. A tree-based API allows unlimited, random access and manipulation, while an event-based API is a one-time pass through the source document.

Difference Table

The following table depicts the difference in the features amongst the available XML Parsing APIs:




API TypeAPI Type is Pull, streamingAPI Type is Push, streaming
Ease of UseStAX have Usability HighSAX Have Usability Medium
XPath Capability StAX has No XPath Capability SAX has No XPath Capability 
CPU and Memory EfficiencyCPU and Memory Efficiency is GoodCPU and Memory Efficiency is Good
Forward OnlyThe Forward Only Feature is PresentThe Forward Only Feature is Present
Read XMLThe Feature of Reading XML is PresentThe Feature of Reading XML is Present
Write XMLThe Feature of Writing XML is PresentThe Feature of Writing XML is Not Present
Create, Read, Update, DeleteThe Command Features like Create, Read, Update, Delete are Not PresentThe Command Features like Create, Read, Update, Delete are Not Present
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!