aspose file tools*
The moose likes XML and Related Technologies and the fly likes Searching using SAX !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Searching using SAX !!" Watch "Searching using SAX !!" New topic
Author

Searching using SAX !!

Parth Bhatt
Ranch Hand

Joined: Oct 19, 2005
Posts: 58
hi,
I'm building context specific help for a project that i'm working on & The complete Help related data is stored in an XML file which is of the following format.


The task is to write a Java Program that has a method that takes a help ID & returns the Help-Topic & Help-Data associated with it.

I believe SAX would be the write thing to use as the file is expected to be very large & i am aware of the events that a SAX Parser generates. Here's the skeleton code that i plan to use.


Please suggest the right way to search for the Help-Topic & Help-Data for a given ID.


If your new Big Idea doesn't scare the hell out of you, <br />it's probably not a "new Big Idea".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18666
    
    8

In your startElement method, if the element name is Help-Item and the ID attribute contains the value you are looking for, set a boolean flag "helpItem" to true. If the element name is Help-Item and the ID attribute is something else, set it to false. If the element name is anything else, leave it alone.

Also in your startElement method, if the element name is Help-Topic, set a boolean flag "helpTopic" to true. If not, set it to false.

And in your startElement method, create an empty StringBuffer.

In your characters method, append the characters to the StringBuffer.

Finally in your endElement method, if helpItem is true and helpTopic is true, then the StringBuffer contains the help topic. Similar logic gives you the help data.

I hope you are not planning to do this search while an on-line user is waiting for you to display its results. If the XML file is large enough that you can't use DOM, it is going to take an annoyingly long time to do the search.
Parth Bhatt
Ranch Hand

Joined: Oct 19, 2005
Posts: 58
Thanks a lot Paul, Looks like i've got the answer...

No, this is for a desktop application. This is how it is actually supposed to work : for certain actions that a user performs with the GUI there is a help ID associated with it... So that the context specific help is displayed for what the user is doing now....!!

for example : if the user clicks on the "SEND MESSAGE" button help regarding sending a message is displayed in a panel.

I opted for SAX as i learnt from other posts in this forum that DOM is slower if the file is too large...

Is SAX ideal to use is this case ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18666
    
    8

DOM may be slower if you re-parse the entire file every time you need some data from it. But why would you do that? The more sensible thing would be to parse the file once, at the beginning of the program, and build a Map that maps help-item-id to something containing help-topic and help-data. You could do this either in SAX or DOM, it wouldn't make much difference.

An even more sensible thing would be to not use XML at all for this application. If you just want help-item-id to be the key for finding help-topic and help-data then something which permits random access to the data would be preferable. Like a database.
 
 
subject: Searching using SAX !!