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 / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

sax parser and nested tags

Thread view: 
grasp06110 - 12 Sep 2006 20:23 GMT
Hi Everybody!

I'm doing some xml stuff and have gone down the road of writting a sax
parser to get data out of the file.  I've over-written the startElement
and characters methods and I am getting the data I need... kind of.  I
am not seeing an easy way to detect where I am in a nested structure
(e.g. a house has a room that has a cabinet that has a name v. a person
has a name).  The stuff I've been reading talks about fully qualified
names.  I would guess that that would give some indication of the
nesting of an element (e.g. house.room.cabenet.name v. person.name).  I
am not seeing this behavior.  In the house example and in the person
example I get "name" as the qualified name.

Any help would be appreciated.  

Thanks,
John
grasp06110 - 12 Sep 2006 21:35 GMT
Hi everybody,

Roll-your-own seem to work prety well.  Are there any potential pit
falls I'm missing with this solution?

This goes in startNode:

       if (this.nodeName_ == null
               || this.nodeName_.trim().length() == 0) {
           this.nodeName_ = name;
       } else {
           this.nodeName_ += "." + name;
       }

This goes in endNode:

       if (this.nodeName_ != null) {
           int start = 0;
           int end = nodeName_.lastIndexOf(".");
           if (end == -1) {
               nodeName_ = "";
           } else {
               nodeName_ = nodeName_.substring(start, end);
           }
       }
Tor Iver Wilhelmsen - 13 Sep 2006 15:54 GMT
> I am not seeing an easy way to detect where I am in a nested
> structure (e.g. a house has a room that has a cabinet that has a
> name v. a person has a name).

Push the elements onto a stack as you go. Peek at the stack to see the
context you're in. Pop when you reach the closing tag.
Lew - 17 Sep 2006 16:23 GMT
>> I am not seeing an easy way to detect where I am in a nested
>> structure (e.g. a house has a room that has a cabinet that has a
>> name v. a person has a name).
>
> Push the elements onto a stack as you go. Peek at the stack to see the
> context you're in. Pop when you reach the closing tag.

You might also explore the jaxb parser library.

Often it is enough just to refer the enclosing element (or its name).

class ElementRepresentation
{
    ElementRepresentation encloser;
    String tag;
    ...
}

Then if you want to distinguish, say, person.name from organization.name you
can put code in the "name" handler like:

    ElementRepresentation name;
    ...
    if ( name.getEncloser().getTag().equals( "person" ) )
    { ... }
    else if ( name.getEncloser().getTag().equals( "organization" ) )
    { ... }
    ...

Crude, but effective.  If necessary you can follow the encloser chain through
more than one layer -
    if ( name.getEncloser().getEncloser().getTag().equals( "supervisor" )) ...

I've implemented a few projects using SAX parsing and this type of strategy,
where DOM parsing or XSL seemed like overkill.  SAX parsing can process an XML
document in a single pass, and can be amazingly fast.  The downside is that it
hard-codes schema structure into the logic, not always a Bad Thing but a
limitation nonetheless.

- 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



©2008 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.