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 xml parsing with dom 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 "xml parsing with dom" Watch "xml parsing with dom" New topic
Author

xml parsing with dom

John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 118
Here's an xml question. Suppose I have an xml structure called
defined as follows:

<orders>
<order>
<partNum>12345778A</partNum>
<description>widget controller</description>
<quantity>2</quantity
</order>
<order>
<partNum>67789A2C</partNum>
<description>widget adapter</description>
<quantity>3</quantity
</order>
. another order
. another order
</orders>

In my xml file, I have several orders. Suppose I'd like to
iterate through and see all the nodes in each order. By doing

NodeList orders = root.getElementsByTagName(�order�);
I�m able to iterate through and print each node name, but when I
try to print the node value, I get null.
Should I cast to an Element instead of node? As you can see, there
no attributes, and I did this intentionally to learn how to pull data
out of a Node.

Thanks,

John
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Well, no. Check the API documentation for the Node interface and you'll see what the "node value" is for each type of Node. For an Element node, the value is indeed null.

Most likely you were looking for the value of the Element's child, which is a Text node.
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 118
Ok, then what code can I use to get something like

partNum === 1234577A
description === "blah, blah"
quantity === 2

Iterating I can print out partNum, description, and quantity
by using node.getNodeName() method, however, when I do
node.getNodeValue(), I get null back and clearly, there is
SOMETHING there.

John
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

You can either use getTextContent() to get the element value or iterate the element node and then use getNodeValue().

Below is an example to print the element and its corresponding value. Take a note that this is only an example. You can do a much better approach, such as using recursion, etc.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 118
Freddy,

Thanks, I totally passed by the getTextContent() method when
I was looking at the api...I was more focused on the getNodeValue()
call that I missed it.

Again, thanks.

John
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

That will work well for your particular question. But note that getTextContent() does exactly what it says, and that might not be what you expect in the case of mixed content elements. Try it on the <orders> element of your original post, for example, and see what it produces.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xml parsing with dom