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

xml parsing with dom

 
John Gregory
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Gregory
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic