The SAX Packages: The SAX parser is defined in the following packages.
|org.xml.sax||Defines the SAX interfaces. The name
|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.|
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 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
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
Brief description of the key SAX APIs:
SAXParserinterface 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.
EntityResolverinterfaces (with null methods).You override only the ones you're interested in.
endElementare invoked when an XML tag is recognized. This interface also defines methods
processingInstruction, which are invoked when the parser encounters the text in an XML element or an inline processing instruction, respectively.
warningare 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.
resolveEntitymethod is invoked when the parser needs to identify the data referenced by a URI. .
Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles