File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes XML DOM & children nodes 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 DOM & children nodes" Watch "XML DOM & children nodes" New topic

XML DOM & children nodes

John Low

Joined: Mar 24, 2003
Posts: 15
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 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:

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

Joined: May 26, 2003
Posts: 33102

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Moving to the XML forum.

"JavaRanch, where the deer and the Certified play" - David O'Meara
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
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.
I agree. Here's the link:
subject: XML DOM & children nodes
It's not a secret anymore!