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

XML DOM - Change the node value


May 27, 2021 XML DOM


Table of contents


XML DOM changes the node value

When using DOM, you can modify node values as needed, as detailed in this section.

The nodeValue property is used to change the node value.

The setAttribute() method is used to change property values.


XML DOM - Change the node value

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.

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

Change property values by using setAttribute
This example uses the setAttribute() method to change the value of the "category" property of the first slt;book.

Change property values by using nodeValue
This example uses the nodeValue property to change the value of the "category" property of the first slt;book.


Change 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 change element text is to change the value of this child node (text node).


Change the value of the text node

The nodeValue property can be used to change the value of a text node.

The following snippet changes the text node value 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
  3. Change the node value of the text node to Easy Cooking

Traversing and changing the text nodes of all the elements: Try


Change the value of the property

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

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

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


Change the property by using setAttribute().

The setAttribute() method changes the value of an existing property, or creates a new property.

The following code changes the category property of the element:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Get the first element of the slt;book
  3. Change the value of the "category" property to "food"

Traverse all the elements and add a new property: Try

Note: If the property does not exist, a new property (with the specified name and value) is created.


Change the property by using nodeValue

The nodeValue property can be used to change the value of the property node:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="food";

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Gets the "category" property of the first element
  3. Change the value of the property node to "food"

Related articles

HTML DOM