aspose file tools*
The moose likes XML and Related Technologies and the fly likes Searching with SAX in xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Searching with SAX in xml" Watch "Searching with SAX in xml" New topic
Author

Searching with SAX in xml

Sebastian Green
Ranch Hand

Joined: Jan 30, 2004
Posts: 136
I want to search an xml-file using the SAX-parser. Like, show all nodes where the personid-node equals 001. Im having a hard time understanding XML and SAX in generall. Can anyone help me, point me in tha right direction? I have tried to use the ranchs wiki faq tutorial. But it didnt help so much...


/ Sebastian<br />No blog, No homepage, No life?
Hari Vignesh Padmanaban
Ranch Hand

Joined: Oct 07, 2003
Posts: 578
look at this
http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/sax/index.html
hope this helps
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
It might be a lot easier to start with DOM instead of SAX.
With DOM, you get an object tree which you can iterate with for loops etc. and pick the nodes that match into, say, an ArrayList.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Sebastian Green
Ranch Hand

Joined: Jan 30, 2004
Posts: 136
Perhaps I should start with the DOM but the task rquiers using SAX. And the Sun-Tutorial is very long, Im looking for something shorter. I found something at the The Java Developers Almanac (http://www.javaalmanac.com/egs/javax.xml.parsers/BasicSax.html) but its only parse the xml-file. I want to the parser to print out every childnode to every node called <person>.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Even better, use of XPath (as in XSLT) would be better (with Xalan for instance).
The XPath expression for returning all the person whose id nodes equals to 1 would look something like:

For instance, in the following xml document there are a number of person nodes:

The following code will display the person with id 001:

Cheers
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sebastian,
If you'd like to use Sax events, it's another story as no document tree is available.
Given the same xml file as before, the following code uses Sax to render the found person:

Of course this code is incomplete and just fits the simple example, but that should give you an idea of what to do to get the job done.
Cheers
Sebastian Green
Ranch Hand

Joined: Jan 30, 2004
Posts: 136
Thanks for your tips I will check them out right away (have been busy for a while). Im aware of the XPath, Im getting to soon but I need to understand the basic first. The next task will be XPath related. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching with SAX in xml