Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM Node Information

XML DOM Node Information


The nodeName, nodeValue, and nodeType properties contain information about a node.


Try it - Example

The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, used to load the XML file.

Get an Element Node's Node Name
This example uses the nodeName property to get the node name of the root element in "books.xml".

Get Text from a Text Node
This example uses the nodeValue property to get the first element in "books.xml" the text of the element.</p> <p><a href="/try/try.php?filename=try_dom_changeelement">Change the text in a text node</a><br /> This example uses the nodeValue property to change the first element in "books.xml" <title> the text of the element.</p> <p><a href="/try/try.php?filename=try_dom_root">Get the node name and type of an element node</a><br /> This example uses the nodeName and nodeType properties to get the node name and type of the root element in "books.xml".</p> <hr /> <h2>The node's attributes</h2> <p>In the XML DOM, every node is a<strong>Object</strong>.</p> <p>An object has methods and properties, and can be accessed and manipulated through JavaScript.</p> <p>The three important node properties are:</p> <ul> <li>nodeName</li> <li>nodeValue</li> <li>nodeType</li> </ul> <hr /> <h2>nodeName Attribute</h2> <p>The nodeName property specifies the name of a node.</p> <ul> <li>nodeName is read-only</li> <li>the nodeName of an element node is the same as the tag name</li> <li>the nodeName of an attribute node is the name of the attribute</li> <li>the nodeName of a text node is always #text</li> <li>the nodeName of a document node is always #document</li> </ul> <p><a href="/try/try.php?filename=try_dom_nodename3">Try it.</a></p> <hr /> <h2>nodeValue Attribute</h2> <p>The nodeValue property specifies the value of a node.</p> <ul> <li>the nodeValue of an element node is undefined</li> <li>the nodeValue of a text node is the text itself</li> <li>the nodeValue of an attribute node is the value of the attribute</li> </ul> <hr /> <h2>Get the value of an element</h2> <p>The following code retrieves the first <title> the value of the element's text node:</p> <h2>Example</h2> <p>xmlDoc</p> <p>=</p> <p>loadXMLDoc</p> <p>(</p> <p>"</p> <p>books.xml</p> <p>"</p> <p>)</p> <p>;</p> <p>x</p> <p>=</p> <p>xmlDoc</p> <p>.</p> <p>getElementsByTagName</p> <p>(</p> <p>"</p> <p>title</p> <p>"</p> <p>)</p> <p>[</p> <p>0</p> <p>]</p> <p>.</p> <p>childNodes</p> <p>[</p> <p>0</p> <p>]</p> <p>;</p> <p>txt</p> <p>=</p> <p>x</p> <p>.</p> <p>nodeValue</p> <p>;</p> <p>Try it »</p> <p>Result: txt = "Everyday Italian"</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the " books.xml" load into xmlDoc

  • Get the first element node's text node</li>
  • Set the txt variable to the text node's value

  • Change an Element's Value

    The following code changes the first element's text node value:</p>

    Example

    xmlDoc

    =

    loadXMLDoc

    (

    "

    books.xml

    "

    )

    ;

    x

    =

    xmlDoc

    .

    getElementsByTagName

    (

    "

    title

    "

    )

    [

    0

    ]

    .

    childNodes

    [

    0

    ]

    ;

    x

    .

    nodeValue

    =

    "

    Easy Cooking

    "

    ;

    Try it »

    Example explanation:

    1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
    2. Get the first element node's text node</li>
    3. Change the text node's value to "Easy Cooking"

    The nodeType Property

    The nodeType property specifies the type of the node.

    nodeType is read-only.

    The most important node types are:

    Node types NodeType
    Element 1
    property 2
    Text 3
    Notes 8
    Document 9

    Try it.