What is SAX in XML?
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.
Please Login to comment...