Solved

How to fetch child node of  an Element using SAX parser

Posted on 2002-06-03
8
1,655 Views
Last Modified: 2013-11-23
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
Comment
Question by:kgreddy
8 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 50 total points
ID: 7051742
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
 
LVL 7

Expert Comment

by:yoren
ID: 7052116
I meant to say ...until you hit a corresponding ContentHandler.endElement()...
0
 

Expert Comment

by:hardeepbsingh
ID: 7053128
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
Technology Partners: 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!

 
LVL 2

Author Comment

by:kgreddy
ID: 7053252
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
 

Assisted Solution

by:hardeepbsingh
hardeepbsingh earned 50 total points
ID: 7053260
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
 
LVL 7

Expert Comment

by:yoren
ID: 7053300
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
 
LVL 1

Expert Comment

by:howesd
ID: 7054672
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
 
LVL 35

Expert Comment

by:girionis
ID: 8833951
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question