• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Confusions about a DOM hierarchy

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a XML file which contains
----------------------------------------------------------
<Person>
<Good>
<Name1>XXX</Name1>
<Name2>YYY</Name2>
</Good>
<Bad>
<Name3>ZZZ</Name3>
</Bad>
<Excellent>WWW</Excellent>
</Person>
-----------------------------------------------------------

I have used a DOMParser to parse and create a document named doc from this file.

Then I have done
-----------------------------------------------------------
NodeList nl = doc.getElementsByTagName("Person");
------------------------------------------------------------

The number of nodes in the node list is 7.

Upto this everything is fine. But I am getting confused whenever I am trying to access the nodes by getNodeName() and getNodevalue() in the nodelist.

Will u please help me to understand the tree view of this document? I mean, how the XML tags are represented in the nodelist?


Please help.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhra Chandra:
I have a XML file which contains
Then I have done

NodeList nl = doc.getElementsByTagName("Person");


The number of nodes in the node list is 7.

Upto this everything is fine....



When I do this, I see that nl.getLength() returns one '1'.
You said number of nodes in node list is 7! And that's fine!
What am I missing ?

- m
[ January 28, 2005: Message edited by: Madhav Lakkapragada ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will u please help me to understand the tree view of this document? I mean, how the XML tags are represented in the nodelist?


The NodeList is a collection of Element objects - each named "Person".

I suspect that your are confused because the result of getNodeValue on these elements is null. That is because the contents you are probably looking for is in Text type nodes that are children of "Name1" etc Elements that are in turn children of Good and Bad Elements that are children of Person elements.

Pulling the data out will involve lots of getFirstChild() and similar calls.

The java API for the org.w3c.dom package - Node interface, has a lovely table showing what the node name and value are for every kind of XML node.
Bill
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic