Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get data from Document object

 
Andrew Sljusar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I am using Crimson parser to parse xml.
My xml:
******************************
<root>
<aaa>
<bbb>
<info>bbb information</info>
</bbb>
<info>aaa information</info>
</aaa>
</root>
*********************************
After parsing this xml I have Document object;
Can you tell me how can I get "aaa information" from Document object not "bbb information"?
Thank you.
Andrew Sljusar.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
There are two ways of getting this information.
1. Using the DOM APIs
2. Using the XPath APIs
Example using DOM:

Example using XPath (requires XALAN):

Cheers
 
Andrew Sljusar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
But if my xml has the following structure:
******************************
<root>
<aaa>
<info>aaa information</info>
<bbb>
<info>bbb information</info>
</bbb>
</aaa>
</root>
*********************************
then this code returns: "bbb information"
NodeList infoNodeList = doc.getElementsByTagName("info");
Element info = (Element) infoNodeList.item(1);
Text text = (Text) info.getFirstChild();
String aaaInformation = text.getNodeValue();
How can I get data from "/root/aaa/info" not from "/root/aaa/bbb/info" using DOM Api?
regards,
Andrew Sljusar
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shouldn't that be infoNodeList.item(0) instead of (1) ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic