Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM node information


May 27, 2021 XML DOM


Table of contents


XML DOM node information


DOM node information refers to the fact that in XML and DOM, each node is an object.

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


XML DOM node information

Try it - instance

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

Gets the node name of the element node
This example uses the nodeName property to get the .xml name of the root element in "books."

Get text from the text node
This example uses the nodeValue property to get the text .xml the first element in the "books.xml " .

Change the text in the text node
This example uses the nodeValue property to change the text .xml the first element in the "books".

Gets the node name and type of the element node
This example uses the nodeName and nodeType properties to get the node name .xml the root element in "books and books".


The property of the node

In XML DOM, each node is a single object.

Objects have methods and properties that can be accessed and operated through JavaScript.

The three important node properties are:

  • nodeName
  • nodeValue
  • nodeType

nodeName property

The nodeName property specifies the name of the node.

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

Give it a try.


nodeValue property

The nodeValue property specifies 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 of the property node is the value of the property

Gets the value of the element

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

xmlDoc=loadXMLDoc("books.xml");

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

Try it out . . .

RESULTS: txt - "Everyday Italian"

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets the text node of the first element node
  3. Set the txt variable 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 element:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets the text node of the first element node
  3. Change the value of the text node to Easy Cooking

nodeType property

The nodeType property specifies the type of node.

nodeType is read-only.

The most important node types are:

Node type NodeType
element 1
Attributes 2
text 3
Comment 8
Documentation 9

Give it a try.

In the next section, we'll show you the nodes of XML DOM;