XML DOM tutoria...login
XML DOM tutorial
author:php.cn  update time:2022-04-13 15:27:56

DOM change node


XML DOM Change node value


The nodeValue attribute is used to change the node value.

The setAttribute() method is used to change the attribute value.



tryitimg.gifTry it out - Example

The example below uses the XML file books.xml.
Function loadXMLDoc(), located in external JavaScript, is used to load XML files.

Change the text node of the element
This example uses the nodeValue attribute to change the text node of the first <title> element in "books.xml".

Change the attribute value by using setAttribute
This example uses the setAttribute() method to change the value of the "category" attribute of the first <book>.

Change the attribute value by using nodeValue
This example uses the nodeValue attribute to change the value of the "category" attribute of the first <book>.


Change the value of an element

In the DOM, each component is a node. Element nodes have no text value.

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

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


Changing the value of a 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

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

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

document.write(x.nodeValue);
</script>
</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

Explanation of the example:

  1. Use loadXMLDoc() to load "books .xml" Load xmlDoc

  2. Get the text node of the first <title> element

  3. Put the node value of the text node Change to "Easy Cooking"

Loop through and change the text nodes of all <title> elements: try it


Change the value of the attribute

In DOM, attributes are also nodes. Unlike element nodes, attribute nodes have text values. I

The way to change the value of an attribute is to change its text value.

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


Changing attributes 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

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');

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

document.write(x[0].getAttribute("category"));
</script>
</body>
</html>

Run Example»

Click the "Run Instance" button to view the online instance

Explanation of examples:

  1. Use loadXMLDoc() to load "books.xml" into xmlDoc

  2. Get the first < book> element

  3. Change the value of the "category" attribute to "food"

Traverse all the <title> elements and add a New property: Try it

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


Changing attributes by using nodeValue

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

Example

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

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

document.write(y.nodeValue);
</script>
</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance

Explanation of the instance:

  1. Use loadXMLDoc() to load "books.xml" into xmlDoc

  2. Get the "category" attribute of the first <book> element

  3. Change the value of this attribute node to "food"


php.cn