JAXP SAX: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 21: Line 21:
SAX parsers have low memory requirements, as they don't construct an internal representation of the XML document.
SAX parsers have low memory requirements, as they don't construct an internal representation of the XML document.


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


=SAX Example=
=SAX Examples=


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[SAX Example]]
:[[SAX Examples]]
</blockquote>
</blockquote>



Revision as of 02:14, 10 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. Note that JAXP DOM and SAX parsers handle errors in a similar manner, the same exceptions are generated so the error handling code is virtually identical.

SAX parsers have low memory requirements, as they don't construct an internal representation of the XML document.

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

SAX Examples

SAX Examples

Component Packages