SAX Interface
The SAX parser is called the SAXParser
and is created by the javax.xml.parsers.SAXParserFactory
. Unlike the DOM parser, the SAX parser does not create an in-memory representation of the XML document and so is faster and uses less memory. Instead, the SAX parser informs clients of the XML document structure by invoking callbacks, that is, by invoking methods on a org.xml.sax.helpers.DefaultHandler
instance provided to the parser. This way of accessing document is called Streaming XML.
The DefaultHandler
class implements the ContentHandler
, the ErrorHandler
, the DTDHandler
, and the EntityResolver
interfaces. Most clients will be interested in methods defined in the ContentHandler
interface that are called when the SAX parser encounters the corresponding elements in the XML document. The most important methods in this interface are:
startDocument
andendDocument
methods that are called at the start and end of a XML document.startElement
andendElement
methods that are called at the start and end of a document element.characters
method that is called with the text data contents contained between the start and end tags of an XML document element.
Clients provide a subclass of the DefaultHandler
that overrides these methods and processes the data. This may involve storing the data into a database or writing it out to a stream.
During parsing, the parser may need to access external documents. It is possible to store a local cache for frequently used documents using an XML Catalog.
This was introduced with Java 1.3 in May 2000.
Read more about this topic: Java API For XML Processing