Solved

How to fetch child node of  an Element using SAX parser

Posted on 2002-06-03
8
1,689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 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