Fundamentals of Events-Based Parsing
A version of this page is also available for
4/8/2010
To appreciate the benefits of the Microsoft® SAX2 COM implementation, 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, it 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 SAX2, the parser, not a user, generates the events.
For 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 as shown in the following code example.
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.