GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes How to retrieve reqd. tag values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to retrieve reqd. tag values" Watch "How to retrieve reqd. tag values" New topic
Author

How to retrieve reqd. tag values

Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
Hi,
Iam using Xereces.
How to retrieve reqd. tags and their child elements in dynamic way. In the XML given below how to retrieve "Manifest" element and all the child element values in dynamic way. Is there any method with just passing tag name and getting all the child elements and their attribute values in collections(eg., Vector/Hashtable)


Thanks,
Anil


SCJP 1.5, SCEA, ICED (287,484,486)
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
Any ideas???
Thanks,
Anil
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Anil,
To do this, you can do the following:
1. Load the document into a DOM tree (using JAXP would abstract you from using a SAX or DOM or DOM parser)
2. Locate the manifest node using an XPath expression
3. Traverse the element using the DOM API
Here is some a code extract to do 1 & 2, 3 would depend on what you intend to do with the manifest information.

Please tell me if this helps.
Best regards,
Beno�t
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
Hi Beno�t,
Even i did the same thing to build DOM tree with Xerces as below


So finally i get the Vector of Hashtables in return.
If i pass "item" in the getAllElements(ElementName) method i will get all the occurances of "item" element in vector and their
corresponding attribute name(as a key in Hashtable) and attribute value( as a value in Hashtable).
My reqt. is to get in a single vector of <item> elements, which has hashtable with element name(as a key) and element value(as a value) and if there are any attributes under this, that should have one more hashtable with attr.name and value.
Does XPath solve this problem.
Or
Can we do it with Xerces alone
How XPath will be useful

Any ideas?

Thanks,
Anil

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Why would you want to do that? A DOM node already stores all the information you need.
Anyway, you already know how to traverse the DOM tree (as your example shows) and I'm sure you're good enough to come up with the right algorithm for this on your own.
Regards,
Beno�t
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
I found out JDOM can solve this problem.

 
GeeCON Prague 2014
 
subject: How to retrieve reqd. tag values