Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extract data between two xml nodes with SAX Parser

 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have question about extracting xml data between two specific nodes with SAX Parser only.

Xml for your reference:


How can i get the data between <state id="product"></state> with all the child node. I dont want to use the dom xpath.I want to utilise the SAX parser.

Regards
Jatan
 
g tsuji
Ranch Hand
Posts: 669
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i get the data between <state id="product"></state> with all the child node. I dont want to use the dom xpath.I want to utilise the SAX parser.

First you know how to get _all_ the data, whatever it means, using SAX parser, I suppose.

With that as the base, you impose conditions from within the method startElement() of the Content Handler... verifying [1] the qualified name being equal to "state", and that [2] there exists an attribute named "id" and that [3] the latter being of value "product". With all these conditions tested positive, you raise a flag or whatever and you start storing/displaying all the results in whatever format used in the way getting _all_ the data mentioned above with all methods including startElement() always checking the flag for the green light to store/display data.

And then, in the endElement() method you verify if the qname being "state" or not. If yes, reset the flag to stop storing/displaying data... If no, just leave the flag alone.

That is the general layout if not an oversimplified description.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic