Solved

How to fetch child node of  an Element using SAX parser

Posted on 2002-06-03
8
1,542 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now