File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes get data from Document object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "get data from Document object" Watch "get data from Document object" New topic
Author

get data from Document object

Andrew Sljusar
Greenhorn

Joined: Nov 04, 2002
Posts: 3
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 04, 2002
Posts: 3
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

Joined: Jun 06, 2002
Posts: 1056
shouldn't that be infoNodeList.item(0) instead of (1) ?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
 
subject: get data from Document object
 
Similar Threads
simple example problem
Ned help with "XPath"
Xpath
copy nodes using copy-of
Difference between count() and last()