XML DOM - changing node values
nodeValue property is used to change the node values.
setAttribute () method is used to change the property value.
Try - Example
The following example uses XML files the Books.xml .
Function loadXMLDoc () , in an external JavaScript is used to load the XML file.
Change the text of an element node
This example uses the nodeValue property to change the text node "books.xml" the first <title> element.
By using setAttribute to change the property value
This example uses the value setAttribute () method to change the first <book> the "category" attribute.
By using nodeValue to change the property value
This example uses the nodeValue attribute values to change the first <book> the "category" attribute.
Change the value of the element
In the DOM, everything is a node. No text element node values.
Element node text is stored in the child node. This node is called a text node.
Change the text of an element, it is to change the value of the child node (text node).
Change the value of the text node
nodeValue property can be used to change the value of the text node.
The following code fragment changes the text node value of the first <title> element:
Examples
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Gets the first text node <title> element
- The node of the text node's value is changed to "Easy Cooking"
Through and change all the <title> element of the text node: Try
Change the value of the property
In the DOM, the property is also a node. Unlike element nodes, attribute nodes have text values. I
Change the property value method is to change its text value.
By using the setAttribute () method or attribute node nodeValue property to accomplish this task.
Change properties by using setAttribute ()
setAttribute () method to change the existing value of the property, or to create new properties.
The following code change category Properties <book> element:
Examples
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Get the first <book> element
- Change the value of the "category" attribute of the "food"
Through all <title> elements and add a new property: try
Note: If theproperty does not exist, create a new property (has the specified name and value).
Change properties by using nodeValue
nodeValue property can be used to change the value of an attribute node:
Examples
x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="food";
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Get the first <book> element "category" attribute
- Change the value of this attribute node is "food"