SAX Architecture


In SAX, you can configure the parser with a variety of callback handlers, as shown in Figure 13-1.

image from book
Figure 13-1

When the parser scans an external stream that contains XML markup, it reports the various events involved to those callback handlers. These events include but are not limited to the following:

  • q Beginning of the document

  • q End of the document

  • q Namespace mapping

  • q Errors in well-formedness

  • q Validation errors

  • q Text data

  • q Start of an element

  • q End of an element

The SAX API provides interfaces that define the contract for these callback handlers. When you write XML applications that use SAX, you can write implementations for these interfaces and register them with a SAX parser.

Basic SAX Application Flow

Every SAX application goes through the same basic steps to process an XML document.

  1. It obtains a reference to an object that implements the XMLReader interface.

  2. It creates an instance of an application-specific object that implements one or more of the various SAX *Handler (DTDHandler, ContentHandler, ErrorHandler, and EntityResolver) interfaces.

  3. IT registers the object instance with the XMLReader object so that it will receive notifications as XML parsing events occur.

  4. It calls the XMLReader.parse() method for each XML document that needs to be processed by the application. The object instance that was registered in Step 3 receives notifications progressively as the document is parsed.

It is up to the application-specific object (or objects) to track and process the information that is delivered via the various event notification methods that it implements. For example, an application that wants to strip markup out of an XML document and leave only the text content must implement the ContentHandler interface and its specific processing in the characters() event callback method. Even though the setup for every SAX application is almost identical, the data structures and algorithms that process the event notifications vary widely depending on what the application is designed to do.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net