JAXP SAX: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: Line 13:
SAX parsers can only be used for state-independent processing, where the handling of an element does not depend on elements that came before, unlike [[JAXP StAX|StAX]], which can be used for state-dependent processing.
SAX parsers can only be used for state-independent processing, where the handling of an element does not depend on elements that came before, unlike [[JAXP StAX|StAX]], which can be used for state-dependent processing.


To use a SAX parser, use <tt>SAXParserFactory</tt> to generate a parser instance. The actual implementation of the parser implements the <tt>SAXParser</tt> interface and is determined by the value of <tt>javax.xml.parsers.SAXParserFactory</tt> system property. Then call parsers's parse() method. The parser contains a <tt>SAXReader</tt> instance, which invokes callback methods the application must implement. The methods are defined by the <tt>ContentHandler</tt>, <tt>ErrorHandler</tt>, <tt>DTDHandler</tt> and <tt>EntityResolver</tt> interfaces.
Start by generating a parser instance with <tt>SAXParserFactory</tt>. The actual implementation of the parser implements the <tt>SAXParser</tt> interface and is determined by the value of <tt>javax.xml.parsers.SAXParserFactory</tt> system property. Then call parsers's parse() method. The parser contains a <tt>SAXReader</tt> instance, which invokes callback methods the application must implement. The methods are defined by the <tt>ContentHandler</tt>, <tt>ErrorHandler</tt>, <tt>DTDHandler</tt> and <tt>EntityResolver</tt> interfaces.


When an XML tag is recognized, the parser invokes the corresponding methods (<tt>startDocument()</tt>, <tt>startElement()</tt>), ...) on the <tt>ContentHandler</tt> implementation.  
When an XML tag is recognized, the parser invokes the corresponding methods (<tt>startDocument()</tt>, <tt>startElement()</tt>), ...) on the <tt>ContentHandler</tt> implementation.  

Revision as of 21:00, 9 November 2016

External

Internal

Overview

A SAX parser implements event-driven, serial-access push parsing.

SAX parsers can only be used for state-independent processing, where the handling of an element does not depend on elements that came before, unlike StAX, which can be used for state-dependent processing.

Start by generating a parser instance with SAXParserFactory. The actual implementation of the parser implements the SAXParser interface and is determined by the value of javax.xml.parsers.SAXParserFactory system property. Then call parsers's parse() method. The parser contains a SAXReader instance, which invokes callback methods the application must implement. The methods are defined by the ContentHandler, ErrorHandler, DTDHandler and EntityResolver interfaces.

When an XML tag is recognized, the parser invokes the corresponding methods (startDocument(), startElement()), ...) on the ContentHandler implementation.

The ErrorHandler implementation is messaged on various parsing errors. The default implementation is rudimentary, if a more nuanced behavior is necessary, the handler must be implemented.

For a working example of SAX parsing, see SAX Example below.

SAX Example

SAX Example

Component Packages