The SAX Packages: The SAX parser is defined in the following packages.
| Package | Description |
| org.xml.sax | Defines the SAX interfaces. The name
"org.xml" is the package prefix that was settled
on by the group that defined the SAX API. |
| org.xml.sax.ext | Defines SAX extensions that are used when doing more sophisticated SAX processing, for example, to process a document type definitions (DTD) or to see the detailed syntax for a file. |
| org.xml.sax.helpers |
Contains helper classes that make it easier to use SAX -- for example, by defining a default handler that has null-methods for all of the interfaces, so you only need to override the ones you actually want to implement. |
| javax.xml.parsers | Defines the SAXParserFactory
class which returns the SAXParser. Also defines exception classes for
reporting errors. |
javax.xml.parsers Package : Describing the main classes needed here
| SAXParser | Defines the API that wraps an XMLReader implementation class |
| SAXParserFactory | Defines a factory API that enables applications to configure and obtain a SAX based parser to parse XML documents |
org.xml.sax Package : Describing few interfaces
| ContentHandler | Receive notification of the logical content of a document. |
| DTDHandler | Receive notification of basic DTD-related events. |
| EntityResolver | Basic interface for resolving entities. |
| ErrorHandler | Basic interface for SAX error handlers. |
org.xml.sax.helpers Package : Describing the needed interface
| DefaultHandler | Default base class for SAX2 event handlers. |
Understanding SAX Parser
At the very first, create an instance of the SAXParserFactory
class which generates an instance of the parser. This parser wraps a SAXReader object. When the parser's parse()
method is invoked, the reader invokes one of the several callback methods (implemented in the
application). These callback methods are defined by the interfaces ContentHandler,
ErrorHandler, DTDHandler, and EntityResolver.
![]() |
Brief description of the key SAX APIs:
SAXParserFactory
javax.xml.parsers.SAXParserFactory.
SAXParser
SAXParser
interface defines several kinds of parse() methods.
Generally, XML data source and a DefaultHandler object is passed to the
parser. This parser processes the XML file and invokes the appropriate
method on the handler object.
ContentHandler, ErrorHandler, DTDHandler,
and EntityResolver interfaces (with null methods).You override only the ones you're interested in.
ContentHandler
startDocument, endDocument,
startElement, and endElement are invoked when an
XML tag is recognized. This interface also defines methods characters
and processingInstruction, which are invoked when the parser
encounters the text in an XML element or an inline processing instruction,
respectively.
ErrorHandler
error, fatalError,
and warning are invoked in response to various parsing errors.
The default error handler throws an exception for fatal errors and ignores
other errors (including validation errors). To ensure the correct
handling, you'll need to supply your own error handler to the parser.
DTDHandler
EntityResolver
resolveEntity method is invoked
when the parser needs to identify the data referenced by a URI. . If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: The Simple API for XML (SAX) APIs
Post your Comment