XML DOM Change node value
The nodeValue property is used to change the value of a node.
The setAttribute() method is used to change attribute values.
Try it Yourself - Example
The following example uses an XML filebooks.xml。
FunctionloadXMLDoc(), located in external JavaScript, is used to load the XML file.
Change the element's text node
This example uses the nodeValue property to change the text node of the first <title> element in "books.xml".
Change attribute values by using setAttribute
This example uses the setAttribute() method to change the value of the "category" attribute of the first <book>.
Change attribute values by using nodeValue
This example uses the nodeValue property to change the value of the "category" attribute of the first <book>.
Change the value of an element
In the DOM, every component is a node. Element nodes do not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to change an element's text is to change the value of this child node (the 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 code snippet changes the text node value of the first <title> element:
Example
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Get the text node of the first <title> element
- Change the node value of that text node to "Easy Cooking"
Loop through and change the text nodes of all <title> elements:Try it Yourself
Change the value of an attribute
In the DOM, attributes are also nodes. Unlike element nodes, attribute nodes have text values.
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 by using setAttribute()
The setAttribute() method changes the value of an existing attribute, or creates a new attribute.
The following code changes the category attribute of the <book> element:
Example
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Get the first <book> element
- Change the value of the "category" attribute to "food"
Loop through all <title> elements and add a new attribute:Try it Yourself
Note:If the attribute does not exist, a new attribute (with the specified name and value) is created.
Changing an attribute by using nodeValue
The nodeValue property can be used to change the value of an attribute node:
Example
x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="food";
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Get the "category" attribute of the first <book> element
- Change the value of that attribute node to "food"
other extensions