The Simple API for XML (SAX) APIs

This page discusses - The Simple API for XML (SAX) APIs

Ads

Tutorials   
JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank DOM Document
Ads

The Simple API for XML (SAX) APIs

     

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
SAXParserFactory object creates an instance of the parser determined by the system property, using the class javax.xml.parsers.SAXParserFactory.
 
SAXParser
The 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.
 
SAXReader
The SAXParser wraps a SAXReader (may use SAXParser's getXMLReader() and configure it). It is the SAXReader which carries on the conversation with the SAX event handlers you define.
 
DefaultHandler
Not shown in the diagram, a DefaultHandler implements the ContentHandler, ErrorHandler, DTDHandler, and EntityResolver interfaces (with null methods).You override only the ones you're interested in.
 
ContentHandler
Methods like 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
Methods 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
Defines methods you will rarely call. Used while  processing a DTD to recognize and act on declarations for an unparsed entity.
 
EntityResolver
The resolveEntity method is invoked when the parser needs to identify the data referenced by a URI. .

     

Advertisements

Ads
Share on Google+Share on Google+

The Simple API for XML (SAX) APIs

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: The Simple API for XML (SAX) APIs  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads