October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

SAX - The Simple API for XML

  • March 7, 2001
  • By Developer.com Staff
  • Send Email »
  • More Articles »

The Simple API for XML (SAX is a low-level, event-driven or event-based system which operates in a fashion similar to a Graphical User Interface (GUI). For instance, when a start tag is encountered during parsing, an event can be triggered which executes your event handler or callback function to handle the event. One benefit of this method is that it requires much less memory and resources than the DOM does because SAX doesn't store the document in memory. This can be extremely useful when dealing with very large XML documents. Another advantage to using the SAX callback functionality is that you can implement your own data structures for internally storing the document. Therefore, you don't have to make a copy of the parser's tree structure into one of your own structure, as you would have done with the DOM.

In the following sample code fragments, you will see some of the available event handlers, using the Apache.org parser. The following is a slightly modified version of the examples provided with the Apache.org parser. Here, the SAXHandlers class in the class header file encapsulates the event handlers for both document and error events.

#include <sax/HandlerBase.hpp>
class AttributeList;

class SAXHandlers : public HandlerBase
{
public:
    SAXHandlers();
    ~SAXHandlers();

    // Document handlers
    void startElement(const XMLCh* const name, 
                        AttributeList& attributes);
    void characters(const XMLCh* const chars, 
                        const unsigned int length);
    void ignorableWhitespace(const XMLCh* const chars, 
                        const unsigned int length);
    void resetDocument();

    // Error handlers
    void warning(const SAXParseException& exception);
    void error(const SAXParseException& exception);
    void fatalError(const SAXParseException& exception);
};

Using this class, the

handler
object is registered for both document events and error events:

// Initialize the parser
XMLPlatformUtils::Initialize();

// Create the parser
SAXParser parser;

// Create and register the handler
SAXHandlers handler;
parser.setDocumentHandler(&handler);
parser.setErrorHandler(&handler);

// Parse the document
parser.parse(InputFile);

As this code executes the

parse
method, events are triggered to the
handler
object, enabling the application to perform application-specific work for the registered events. For example, the
startElement
method will be called each time the parser encounters the start tag of an element.

Many parsers support SAX, so if you prefer event-driven systems, this may be a reasonable choice for you. Since SOAP serializers and deserializers need to operate quickly, it is within reason to state that a SAX implementation would be better for SOAP.

About the Authors

Kennard Scribner and Mark C. Stiver are the authors of Understanding SOAP (Click to buy) a book by Sams Publishing. This article is based on information from their book.

Kennard Scribner is a principle software consultant specializing in COM and component development. He is also founder and president of EnduraSoft Corporation, a software company specializing in the creation of custom components.

Mark C. Stiver is a consulting software engineer with one of the world's largest supplies of information services for the legal and business industry. Currently he is involved in the design and development of XML-based interfaces for integrating large-scale systems.

© Copyright Sams Publishing, All rights reserved






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel