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

XML DOM - Gets the node value


May 27, 2021 XML DOM


Table of contents


XML DOM gets the node value

In this section, you'll learn how to get node values in the DOM in a specific way.

The nodeValue property is used to get the text value of the node.

The getAttribute() method returns the value of the property.


Gets the value of the element

In doM, each component is a node. The element node does not have a text value.

The text of the element node is stored in the child node. This node is called a text node.

The way to get element text is to get the value of this child node (text node).


Gets the element value

The getElementsByTagName() method returns a list of nodes that contain all the elements that have the specified label name, in the order in which they appear in the source document.

The following code loads "books.xml" into xmlDoc by using loadXMLDoc() and retrieves the first element:

xmlDoc=loadXMLDoc("books.xml");

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

The childNodes property returns a list of child nodes. T he element has only one child node. It is a text node.

The following code retrieves the text nodes of the element:

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

The nodeValue property returns the text value of the text node:

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

Try it out . . .

RESULTS: txt - "Everyday Italian"

Traversing all the elements: Try


Gets the value of the property

In DOM, properties are also nodes. Unlike element nodes, property nodes have text values.

The way to get the value of a property is to get its text value.

You can do this by using the getAttribute() method or the nodeValue property of the property node.


Get property values - getAttribute()

The getAttribute() method returns the property value.

The following code retrieves the text value of the "lang" property of the first element:

xmlDoc=loadXMLDoc("books.xml");

txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Try it out . . .

Result: txt s "en"

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Set the txt variable to the value of the "lang" property of the first title element node

Traverse all the elements and get their "category" property: Try it


Get property values - getAttributeNode()

The getAttributeNode() method returns the property node.

The following code retrieves the text value of the "lang" property of the first element:

xmlDoc=loadXMLDoc("books.xml");

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

Try it out . . .

Result: Result: txt s "en"

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets the "lang" property node of the first element node
  3. Set the txt variable to the value of the property

Traversing all the elements and getting their "category" properties: Try it

That's how doM gets node values, and in the next section, continue to learn how to change node values.