• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xml parsing with dom

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic