XML DOM Node Information

The information about nodes is present in the nodeName, nodeValue, and nodeType properties.

Node Properties:

A node in an XML DOM is an object with methods and properties to be accessed and managed by JavaScript. The major node properties are:

  • nodeName
  • nodeValue
  • nodeType

The nodeName Property:

To specify the name of a node, the nodeName property is used. The nodeName property is read-only. For an element node, it is the same as the tag name. For an attribute node, it is the attribute name. For a text node, the nodeName is always #text, and for the document node, it is #document.

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    Author 3
    Author 4
    2004
    350.00
  


Example:





Output:

The nodeValue Property:

To specify the value of a node, the nodeValue property is used. For element nodes, the nodeValue is undefined, for text nodes, it is in the text itself, and for attribute nodes, it is the attribute value.

Get the Value of an Element:

Here, we will retrieve the text node value of the first <title> element.

Example:





Output:

Explanation:

In the above example, we have loaded “books.xml” into xmlDoc to get the text node of the first <title> element node. For this, we will set the text variable to be the value of the text node.

Change the Value of an Element:

Here, we will change the text node value of the first <title> element.

Example:





Output:

ABC

Alphabets

Explanation:

In the above example, we have loaded “books.xml” into xmlDoc to get the text node of the first <title> element node. We will then change the value of the text node.

The nodeType Property:

To specify the type of node, the nodeType property is used. It is read-only. Most important node types are:

Node type NodeType
Element 1
Attribute 2
Text 3
Comment 8
Document 9

Example:





Output:

Please follow and like us:
Content Protection by DMCA.com