May 27, 2021 XML DOM
1. XML DOM changes the node value
3. Change the value of the element
4. Change the value of the text node
5. Change the value of the property
6. Change the property by using setAttribute().
The nodeValue property is used to change the node value.
The setAttribute() method is used to change property values.
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.
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).
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:
Example explanation:
Traversing and changing the text nodes of all the elements:
Try
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.
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:
Example explanation:
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.
The nodeValue property can be used to change the value of the property node:
Example explanation: