There is an XML and I parse it using the SAX parser. Now suppose I receive a notification at the start of an element startElement(..)
At this time I will know the tag name that the SAX parser is at. But how can I figure out if the current tag is inside a tag called A?
My Thoughts:
One way could be to maintain a boolean variable var insideA=false, and assign it to true on receiving a notification and false on endElement(..). Now when startElement(..) gets called for the current tag I can check if this boolean variable is set to true or false.
I am not sure if there is some other inbuilt way to do this. This could get messy in cases where one has to do this for several tags which will require creating several boolean variables.
Any suggestions for an alternative approach?
- Probably changing to a DOM parser or any other could make things easier?
Yes, that looks like the usual way to address your goal using SAX.
SAX is a bit complex to setup, but "better" than DOM / XPath regarding memory usage. Anyway:
If your XML document fits in RAM,
you can also try using XPath (internally based on the DOM model), which is simpler to use.
As you tagged your question with java, here is a nice tutorial for evaluating XPath expressions in Java, see also the official W3C spec. (a.k.a. recommendation), and this online XPath tester.
If your XML is huge and you find SAX a bit annoying to use,
you can try using StAX, which combines the performance of streaming-based parsing and the usability of "pull parsing" (roughly, no callback anymore).
See also this example tutorial in Java.