Java API for XML (JAXP) – SAX

What is SAX

SAX stands for Simple API for XML. It is an event driven method of accessing elements of an XML document. The elements are accessed serially. The API fire events for each type of data that it finds. The user provides a Handler that can handle the various kinds of events thrown by the parser. The user also needs to provide an error Handler. SAX uses lesser memory than DOM since unlike DOM it does not load the entire document tree into memory.

Important Classes

  • javax.xml.parsers.SAXParserFactory – This is a factory for configuring and obtaining a SAX Parser.
  • javax.xml.parsers.SAXParser – This is an API that wraps a org.xml.sax.XMLReader. The class contains the parse methods that take in an input stream and a Handler for the SAX events.
  • org.xml.sax.XMLReader – This interface defines methods that reads an XML document and provides events that can be acted upon. The SAX parser implements this interface. The interface allows configuring features for the parsers. The API allows setting the DTDHandler, EntityResolver, ContentHandler and ErroHandler
  • org.xml.sax.DTDHandler – This handler receives notification for DTD related events.
  • org.xml.sax.EntityResolver – Resolves entities.
  • org.xml.sax.ErrorHandlerr – Receives notification for warning, error and fatalError encountered during parsing.
  • org.xml.sax.ContentHandler – This receives notification for the various components of the XML. The clients would almost always provide implementation for this interface (or extend the DefaultHandler). The order of events depend on the order of components in the XML Document. The main events are :
    • startDocument-Event thrown during the start of a document parsing
    • endDocument-Event thrown during the End of a document parsing
    • startElement-Event thrown during the start of an Element
    • endElement-Event thrown during the end of an Element
    • characters (char ch[], int start, int l)-Event for Characters. Note that the parser may not return all characters within a particular Text node. The client should read ‘l’ elements from the ‘start’ index.
    • ProcessingInstruction-Event thrown when a Processing Instruction is encountered.


Let us now look at an example. Instead of implementing the four interfaces described above (DTDHandler, EntityResolver,ContenHandler and ErrorHandler) the client would mostly extend the org.xml.sax.helpers.DefaultHandler which implements all the four interafaces.

Java API for XML (JAXP) – SAX

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top