Open In App

What is SAX in XML?

Last Updated : 04 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. XML Text Nodes
  2. XML Element Starts and Ends
  3. XML Processing Instructions
  4. XML Comments

The properties of SAX Parser are depicted below as follows: 

Properties of SAX Parser

 

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




// Java Program to Parse an XML File Using SAX
 
// Importing required classes
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
 
import java.util.*;
import java.io.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args) {
 
        // Try block to check for exceptions
        try {
 
            // Creating an object of SAXParserFactory and
            // SAXParser classes
            SAXParserFactory factory
                = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
 
            // Now creating object of DefaultHandler Class
            DefaultHandler handle = new DefaultHandler() {
 
                boolean bfname = false, blname = false,
                        bscore = false;
 
                // Method
                // To start the element
                public void startElement(
                    String uri, String localName,
                    String qName, Attributes attributes)
                throws SAXException {
 
                    // Print statement
                    System.out.println("Start Element: "
                                       + qName);
 
                    if (qName.equals("firstname"))
                        bfname = true;
                }
            };
        }
 
        // Catch block to handle exceptions
        catch (Exception ex) {
        }
    }
}




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

Similar Reads