XML DOM Node Information

nodeName,nodeValue and nodeType Attributes contain information about the node.

Node attributes

In XML DOM, each node is aObject.

Objects have methods (functions) and properties (information about the object), and can be accessed and manipulated through JavaScript.

Three important XML DOM node attributes are:

  • nodeName
  • nodeValue
  • nodeType

Node name attribute

nodeName The attribute defines the name of the node.

  • nodeName is read-only
  • The nodeName of the element node is the same as the tag name
  • The nodeName of the attribute node is the name of the attribute
  • The nodeName of the text node is always #text
  • The nodeName of the document node is always #document

Try It Yourself

Node value attribute

nodeValue The attribute defines the value of the node.

  • The nodeValue of the element node is undefined
  • The nodeValue of the text node is the text itself
  • The nodeValue attribute of the attribute node is the value of the attribute

Get the value of the element

The following code retrieves the value of the text node of the first <title> element:

Example

var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
var txt = x.nodeValue;

Try It Yourself

Result: txt = "雅舍谈吃"

Example explanation:

  1. Assuming you have already set books.xml loaded to xmlDoc in
  2. Get the text node of the first <title> element
  3. Set txt Variable is set to the value of the text node

Change the value of the element

The following code changes the value of the text node of the first <title> element:

Example

var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue = "潮菜天下";

Try It Yourself

Example explanation:

  1. Assuming you have already set books.xml loaded to xmlDoc in
  2. Get the text node of the first <title> element
  3. Change the value of the text node to "ChaoCaiTianXia"

Node Type Attribute

nodeType The attribute specifies the type of the node.

nodeType It is read-only.

The most important node type is:

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

Try It Yourself