The XML-DEV mailing group developed a Simple API for XML also called the SAX, which is an event-driven online algorithm for parsing XML documents. SAX is a way of reading data from an XML document that is an alternative to the Document Object Model’s mechanism (DOM). Whereas the DOM works on the document as a whole, creating the whole abstract syntax tree of an XML document for the user’s convenience, SAX parsers work on each element of the XML document sequentially, issuing parsing events while passing through the input stream in a single pass. Unlike DOM, SAX does not have a formal specification.
SAX is a programming interface for processing XML files based on events. The DOM’s counterpart, SAX, has a very different way of reading XML code. The Java implementation of SAX is regarded as the de-facto standard. SAX processes documents state-independently, in contrast to DOM which is used for state-dependent processing of XML documents.
Why use SAX Parser
Parsers are used to process XML documents. The parser examines the XML document, checks for errors, and then validate it against a schema or DTD if it’s a validating parser. The next step is determined by the parser in use. It may copy the data into a data structure native to the computer language you’re using on occasion. It may also apply styling to the data or convert it into a presentation format.
Apart from triggering certain events, the SAX parser does nothing with the data. It is up to the SAX parser’s user to decide. The SAX events include (among others) as follows:
- XML Text Nodes
- XML Element Starts and Ends
- XML Processing Instructions
- XML Comments
The properties of SAX Parser are depicted below as follows:
Note: WHEN SAX is better than DOM
- It parses the XML file as a stream rather than allocating RAM for the complete file.
- Since, it uses less memory and is faster than the DOM Parser because the complete file is not stored in memory.
- Therefore, it is considered to be useful in parsing large XML files.
Tip: Drawback of SAX over DOM
- There are no update methods in the SAX Parser. Since the complete file isn’t kept in memory, it is possible to access items only in a sequential manner and the elements cannot be accessed randomly.
Example
Java
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args) {
try {
SAXParserFactory factory
= SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handle = new DefaultHandler() {
boolean bfname = false , blname = false ,
bscore = false ;
public void startElement(
String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
System.out.println( "Start Element: "
+ qName);
if (qName.equals( "firstname" ))
bfname = true ;
}
};
}
catch (Exception ex) {
}
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Nov, 2022
Like Article
Save Article