File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

XML DOM & children nodes

John Low
Greenhorn

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 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

Joined: May 26, 2003
Posts: 31079
    
163

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Cindy Glass
"The Hood"
Sheriff

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
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML DOM & children nodes