Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

SAX PARSING DESIGN PATTERN

Thread view: 
javerra - 28 Mar 2007 18:37 GMT
Hello all.  Just looking for everyones thoughts on this.

I am parsing out an xml document using a sax parser.  I create a new
object representing my XML at the root in the StartElement method of
my record handler and fill the object with the appropriate data at the
EndElement method.

This works fine for for XML structures that contain 0 or 1 child
elements.  My question is for XML elements that can have children with
more that one occurrence what would be the best way to design my
objects.  Is an inner class appropriate for this scenario?  This has
to be a fairly common thing encountered every day but all of the SAX
examples I've see have only worked with very small generic XML
documents.

Your thoughts are appreciated.

Regards
Lew - 29 Mar 2007 00:15 GMT
> Hello all.  Just looking for everyones thoughts on this.
>
[quoted text clipped - 12 lines]
>
> Your thoughts are appreciated.

In the class that implements the parser element for a given tag I include a
reference to the parent parser element object. So if <address> is a valid
subtag for both <person> and <organization>, then the AddressHandler has an
instance variable (inherited from AbstractHandler) called "encloser" or
"parent" or similar that points, respectively, to the current enclosing
PersonHandler or OrganizationHandler.

The parsing loop will retrieve a handler for the current tag during
startElement() (typically using a Map< String, Class<AbstractHandler>>) and
set its "parent" instance variable to the (erstwhile) current AbstractHandler
before pointing currentHandler at the new one. At endElement() you emit
whatever the element has, point the currentHandler to the parent object and
let the (erstwhile) current handler become eligible for GC.

It's a linked list!

By the way, spelling counts. Or does your SAX parser actually have
StartElement() and EndElement() methods? I don't think so. For one thing, if
it did it would not be compatible with Sun's (or Apache's) parser interfaces,
nor the Sun naming conventions.

-- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.