• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML DOM & children nodes

 
John Low
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am parsing a simple xml document using dom and attempting to display the data in a JTree which will eliminate and group the node data together at the parent node level.
the basis for my code is from the java tutorial http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/ with the following modifications to the AdapterNode toString() method:

my xml document has the following basic structure:

the JTree looks like this:

so, given a [group] node which must have [name] and [id] children and optionally a [groups] node for further nesting. however, since the level of detail shown in the JTree is unnecessary for me (all i want is the [group] entry with the nodeValues of it's children who are not a node of name [groups]). so ideally, my JTree should look like:

etc...
the problem is that when i take the parent node ([group]) and getChildNodes(), i can invoke on a node of that list, node.getNodeName() and it will return the proper name of the node. however, if i invoke node.getNodeValue() on the same parent, it returns a null value, despite the fact that when you expand the JTree, the specified nodes and values are present.
sorry for the long message. any ideas anyone?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elements return null for nodeValue(). For that matter, so does everything except attributes, CDATA, comments, processing instructions and text.
You have to get the attributes for the parent node or loop through its children to get any useful info.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the XML forum.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a VERY handy table in the org.w3c.dom.Node JavaDocs that shows what is returned for nodeName and nodeValue for all types of nodes. That null return trips up a lot of people.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic