Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
patrick@intervideo.com
$Date: 2000-09-18 03:38:58-07 $
$Revision: 1.2 $
$State: Exp $
Note :
What Is SAX2?
Have you eve thought that parsing XML documents using DOM (Document
Object Model) is a nightmare ? Here is the remedy : SAX2, the Simple API for
XML, a fast, low-memory alternative to processing XML documents.
SAX2 is a push-model parser. In other words, you provide the handlers and the
parser calls them when a particular event occurs, such as the start of a
document or the start or end of an element. The SAX2 parser generates
several categories of events, including events that occur in the content of the
XML document, events that occur in the DTD, and error events. To handle
these events, you implement a corresponding handler class that contains
methods to process the appropriate events. Note that you only need to
implement handlers for those events you wish to process. If a handler is not
implemented for a specific type of event, the event is simply ignored.
The following is a very simple command-line application that reads an XML file
and prints the file's tags to the console window (There seems to be some errors
in the example code). The application implements only the content handler.
The application consists of the following files:
1 of 6 08/16/2008 10:31 PM
file:///media/T hunder/home/patrick/personal/writing...
// TestSax.cpp
if(!FAILED(hr))
{
MyContent * pMc = new MyContent(); // Set your own content
// handler (and other handlers
// as well).
hr = pRdr->putContentHandler(pMc); // And in real life, check
// this hr!
// MyContent.h
2 of 6 08/16/2008 10:31 PM
file:///media/T hunder/home/patrick/personal/writing...
virtual ~MyContent();
// MyContent.cpp
3 of 6 08/16/2008 10:31 PM
file:///media/T hunder/home/patrick/personal/writing...
Does it seem similar to you ? What pattern have you found in this example ?
Yes, the Visitor pattern. Visitor pattern uses the double dispatching technique
to separate the traversing logic and the processing logic. The traversing logic
lies in implementation of ISAXXMLReader and the processing logic lies in
the implementation of the ISAXContentHandler.
4 of 6 08/16/2008 10:31 PM
file:///media/T hunder/home/patrick/personal/writing...
The Visitor pattern is definitely good when some simple traversing and
processing is needed. On the other hands, if you want to do some complex
editing, Visitor is probably not a good choice because the visitor is a passive
role.
Reference
Design Pattern
Thinking In Java, Bruce Eckel
SAX2 Jumpstart for XML Developers, Eldar A. Musayev
How to traverse DirectShow Graph using Visitor pattern, Tsai Ying-Hau
- I will add this link when I finish it. :-)
Revision History
$Log: SAX2\040a\040good\040example\040of\040Visitor\040pattern.htm,v $
Revision 1.2 2000-09-18 03:38:58-07 patrick
+ Complete summary
+ SAX brief introduction
+ Add pattern illustration diagram
+ More explanations about visitor pattern
+ One more reference link
5 of 6 08/16/2008 10:31 PM
file:///media/T hunder/home/patrick/personal/writing...
* Reformat
6 of 6 08/16/2008 10:31 PM