• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1857
  • Last Modified:

How to fetch child node of an Element using SAX parser

Using SAX parsing method is there any way by which we can get all the children of a particular node. For example, if i am having a xml like this
<a>
  <b>
    <c></c>
 </b>
</a>

Using DOM parsing i can get all the child elements of <a> with getChildNodes(). But since SAX parsing is serial, i am not very sure how can i get the children of a particular element?

Thanks in advance
kgreddy
0
kgreddy
Asked:
kgreddy
2 Solutions
 
yorenCommented:
With SAX you have to figure it out yourself. Add a check in ContentHandler.startElement() to see if it's the "a" node. If so, then you know that everything you get until you hit a corresponding Contenthandler.element() is part of that node.

Depending on your needs, you may have to create your own tree structure. If you have to do more than a few of these you might consider just sticking with DOM (or JDOM, etc.)
0
 
yorenCommented:
I meant to say ...until you hit a corresponding ContentHandler.endElement()...
0
 
hardeepbsinghCommented:
If you only need answers to such queries from the XML file and no other XML processing is done, consider using XPath. Search for Xalan and XPath in google.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kgreddyAuthor Commented:
Hi Yoren,

when StartElement() executes (ContentHandler.startElement()) how will i know, whether the element is having children nodes and grand children node?
is there any way by which i can get all the nodes recursively. If you have seen the parameters of start element, it contains only about attributes.
Thanks
0
 
hardeepbsinghCommented:
You cant know if an element has child nodes or not. You have to store the current element somewhere and wait for more calls to the function with child nodes. You cannot do anything recursively with SAX, thats the very difference. I suggest you stick to DOM since it appears that your needs are essentially random. If you need to access an XML file rather serially or are interested in a small part only, only then you should go for SAX
0
 
yorenCommented:
I agree with hardeep. SAX is a streaming interface. You get the document as it's read. You either have to build your own structures out of the document, or you can use DOM.
0
 
howesdCommented:
I'd certainly have a look at JDOM if I were you - it's very intuitive and "fits" very well with Java. I've little Java experience and next to no XML experience but I very quickly got productive by using it.

Dave
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- split points between yoren and hardeepbsingh

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now