[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to fetch child node of  an Element using SAX parser

Posted on 2002-06-03
8
Medium Priority
?
1,800 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 200 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
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!

 
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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

649 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