Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML DOM question

Reply
Thread Tools

XML DOM question

 
 
john smith
Guest
Posts: n/a
 
      03-30-2005
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.


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-30-2005


john smith wrote:

> 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().

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getNextSibling() never ends? DOM XML nodes (org.w3c.dom) Alan Java 6 10-13-2008 05:48 PM
Replacing _xmlplus.dom.minidom with xml.dom.minidom aine_canby@yahoo.com Python 3 08-03-2007 03:50 PM
Convert a XML DOM Object to a HTML DOM Object manjunath.d@gmail.com XML 0 09-20-2005 08:16 AM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger XML 0 07-28-2004 08:51 AM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger Java 0 07-28-2004 08:51 AM



Advertisments