Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM – Changing Node Values

XML DOM Changing Node Values


The nodeValue property is used to change a node's value.

The setAttribute() method is used to change an attribute's value.


Try it - Example

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

Change an Element's Text Node
This example uses the nodeValue property to change the first element in "books.xml" the text node of the element.</p> <p><a href="/try/try.php?filename=try_dom_setattribute1">Change an attribute value by using setAttribute</a><br /> This example uses the setAttribute() method to change the first <book> The value of the "category" attribute.</p> <p><a href="/try/try.php?filename=try_dom_att_nodevalue">Change an attribute value by using nodeValue</a><br /> This example uses the nodeValue property to change the first <book> The value of the "category" attribute.</p> <hr /> <h2>Change the value of an element</h2> <p>In the DOM, every component is a node. Element nodes have no text value.</p> <p>the text of an element node is stored in a child node. This node is called a text node.</p> <p>The way to change an element's text is to change the value of this child node (the text node).</p> <hr /> <h2>Change the value of a text node</h2> <p>The nodeValue property can be used to change the value of a text node.</p> <p>The following code snippet changes the first <title> the text node value of the element:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p>

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

x.nodeValue="Easy Cooking";

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get the first element's text node</li>
  3. Change the text node's node value to "Easy Cooking"

Loop through and change all element's text nodes: Try it


Change an Attribute's Value

In the DOM, attributes are also nodes. Unlike element nodes, attribute nodes have a text value. I

The way to change an attribute's value is to change its text value.

This can be done by using the setAttribute() method or the nodeValue property of the attribute node.


Change an Attribute Using setAttribute()

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

The following code changes element's category attribute:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');

x[0].setAttribute("category","food");

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get the first Element
  3. Change the "category" attribute's value to "food"

Iterate over all the elements and add a new attribute: Try it

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


Change an Attribute Using nodeValue

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

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book")[0]

y=x.getAttributeNode("category");

y.nodeValue="food";

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get the first element's "category" attribute
  3. Change the attribute node's value to "food"