Open In App

StAX vs SAX Parser in Java

Last Updated : 14 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

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:

Features

StAX

SAX

API Type API Type is Pull, streaming API Type is Push, streaming
Ease of Use StAX have Usability High SAX Have Usability Medium
XPath Capability  StAX has No XPath Capability  SAX has No XPath Capability 
CPU and Memory Efficiency CPU and Memory Efficiency is Good CPU and Memory Efficiency is Good
Forward Only The Forward Only Feature is Present The Forward Only Feature is Present
Read XML The Feature of Reading XML is Present The Feature of Reading XML is Present
Write XML The Feature of Writing XML is Present The Feature of Writing XML is Not Present
Create, Read, Update, Delete The Command Features like Create, Read, Update, Delete are Not Present The Command Features like Create, Read, Update, Delete are Not Present

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads