[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: SAX and stuff ...

  • From: Jack Rusher <jar@i...>
  • To: Tony Pelton <tony.pelton@s...>
  • Date: Tue, 25 Jul 2000 11:27:41 -0700

tony pelton
Tony Pelton wrote:
> 1. Is this a practical, reasonable and expected approach to doing SAX
> parsing for a handful of fairly simple but potentially large XML messages
> that we have brewed "in-house" and have not developed DTD's for ?
> The reason that I ask is that on some of the messages ... I might have 10+
> elements that I am looking for events on ... so that I can "do stuff".
> I then find myself thinking about the number of "if's" my code is performing
> on every startElement() ... and then I find myself putting the "if's" that I
> expect to get more of ... before the one's I expect less of ...

  I use DTDs; I think you should, too.  But, with that in mind, here is
how I do it (all examples in C++):

  o Create an ObjectFactory (in the GoF object sense) that can make
objects based on string names.

  o Make a ParserListener interface that has stuff like this in it:
    // start creating a child object.
  virtual ParserListener* createChildObject( const string object ) = 0;

  // complete a child object.
  virtual ParserListener* completeChildObject( const string object ) =

  // get the value of an object attribute.
  virtual const string getAttribute( const string newKey ) = 0;

  // set attribute "key" to value "value" for this node.
  virtual void setAttribute( const string key, const string value ) = 0;

  // clean up my children after parsetime.
  virtual void arrangeChildren() = 0;

  o Extend the BaseHandler into a class that has a private member that
is a ParserListener, and implement a stack of previous ParserListener

  o StartElement() pushes the current ParserListener onto the stack and
sets it to the return of a call to the current ParserListener's
createChildObject() method.  The createChildObject() method makes a call
to the aforementioned ObjectFactory and returns the new object (which,
itself, is also a ParserListener).

  o EndElement() calls the ParserListener's completeChildObject() method
and pops the old ParserListener off of the stack (reassigning the
current ParserListener to it).  The completeChildObject() calls calls
the object's arrangeChildren() method to do any final cleanup and
reorganization of the tree that was built from the parse.

  o characters() calls the ParserListener's setAttribute() method.

...this approach, when mixed with objects that implement something like
Java's DefaultMutableTreeNode interface, works like a charm to provide a
generic and easily extensible framework to parse documents into internal
tree structures.  I use this technique often for software configuration
files and message passing.  The beauty of it is that the DTD handles
data validation and each object need only know about itself.  It makes
the abstraction(s) very clean from a traditional OO perspective.


Jack Rusher, Senior Engineer | mailto:jar@i...
Integratus, Inc.             | http://www.integratus.com


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.

Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.