XML DOM question

J

john smith

1.) System.out.println("Add this node" + node.getNodeName());
// prints DisplayElement
2.) Node theParentNode = node.getParentNode();
3.) System.out.println(theParentNode);
// prints [Trim: null]
4.) System.out.println("the parent node is " +
theParentNode.getNodeValue()); // prints null


I am having trouble understanding why when the last line prints which I am
thinking should be the parent node is always NULL.

The parent should be "Trim"

Can anyone shed some light on this. This seems pretty straight forward, but
I am confused.

All I am doing is getting the parent node of the DisplayElement node, and it
is always null.

Thanks in advance for any help.
 
M

Martin Honnen

john said:
1.) System.out.println("Add this node" + node.getNodeName());
// prints DisplayElement
2.) Node theParentNode = node.getParentNode();
3.) System.out.println(theParentNode);
// prints [Trim: null]
4.) System.out.println("the parent node is " +
theParentNode.getNodeValue()); // prints null


I am having trouble understanding why when the last line prints which I am
thinking should be the parent node is always NULL.

The parent should be "Trim"

The node value of an element node is alway null in the W3C DOM.
Thus if the node type of the parent node is an element node then the
node value is null.
If you want to read out the text content of an element node then with
W3C DOM Level 3 Core as implemented in Java 1.5 (alias Java 5) you can
use the method
node.getTextContent()
thus if you have
<p>Kibology for <b>all</b>.</p>
and you are looking at the <p> element node you would get
node.getTextContent()
as
"Kibology for all."

If you only have W3C DOM Level 2 support then it is more difficult, you
would need to use a Range or you would need to write a function walking
the child nodes, post back if you need help with that.
See the table in
<http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247>
which describes how nodeName and nodeValue look for the different kind
of nodes.

Or perhaps you are not looking for the content of a node but its name?
Then use node.getNodeName().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top