How Events-Based Parsing Works

 

To appreciate the benefits of the Microsoft COM implementation of the Simple API for XML (SAX), it helps to understand how you can use SAX2 to process XML documents. The SAX2 parser reads an XML document and generates events based on specific symbols that it encounters in the document. The SAX2 parser does not create a tree structure in memory for the document, although you can programmatically instruct it to do so. Rather, the SAX2 parser processes a document's contents sequentially and generates events as it reads through the document.

When you write an application based on events-based programming, you create functions to respond to user-generated events, such as the OnClick event. Writing an application for an events-based parser is similar. However, in the case of SAX, the parser, not a user, generates the events.

Example

Consider the following simplified document.

<?xml version="1.0"?>
<parts>
<part>TurboWidget</part>
</parts>

As the SAX2 parser processes this document, it generates a sequence of events such as the following.

StartDocument( )

StartElement( "parts" )

StartElement( "part" )

Characters( "TurboWidget" )

EndElement( "part" )

EndElement( "parts" )

EndDocument( )

Think of SAX2 as a push parser. SAX2 generates the events and you implement the event handlers that contain methods to process the events. For more information about event handlers and how to implement them, see JumpStart for Creating a SAX2 Application.