OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: SAX and stuff ...

[ Lists Home | Date Index | Thread Index ]
  • From: Jack Rusher <jar@integratus.com>
  • To: Tony Pelton <tony.pelton@spark.com>
  • Date: Tue, 25 Jul 2000 11:27:41 -0700

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:
<interface>
public:
    // start creating a child object.
  virtual ParserListener* createChildObject( const string object ) = 0;

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

  // 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;

protected:
  // clean up my children after parsetime.
  virtual void arrangeChildren() = 0;
</interface>

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

  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.

Yours,

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




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS