XML DOM question

Discussion in 'XML' started by john smith, Mar 30, 2005.

  1. john smith

    john smith Guest

    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.
    john smith, Mar 30, 2005
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Mar 30, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thorsten Meininger
    Replies:
    0
    Views:
    433
    Thorsten Meininger
    Jul 28, 2004
  2. Thorsten Meininger
    Replies:
    0
    Views:
    498
    Thorsten Meininger
    Jul 28, 2004
  3. Replies:
    0
    Views:
    538
  4. Replies:
    3
    Views:
    521
    Stefan Behnel
    Aug 3, 2007
  5. Alan
    Replies:
    6
    Views:
    1,574
Loading...

Share This Page