Dom Nodes Set
# XML DOM: Modifying Node Values
In the XML DOM, every component of an XML document is a node. Modifying the data within an XML document involves changing either the text content of an element or the value of its attributes.
This tutorial explains how to update XML data using the `nodeValue` property and the `setAttribute()` method.
---
## Core Concepts
Before modifying values, it is crucial to understand how the DOM structures elements, text, and attributes:
* **Element Nodes** do not contain text directly. Instead, their text is stored in a child node called a **Text Node**. To change the text of an element, you must modify its child text node.
* **Attribute Nodes** are nodes associated with elements. Unlike element nodes, attribute nodes hold their values directly. You can modify attribute values either by targeting the attribute node itself or by using element-level methods.
---
## Modifying Element Text Values
Because an element's text is stored in a child text node, you must navigate to that text node and update its `nodeValue` property.
### Syntax
```javascript
elementNode.childNodes.nodeValue = "new_value";
// Or more safely, if you know the first child is the text node:
elementNode.firstChild.nodeValue = "new_value";
```
### Code Example
The following example changes the text value of the first `` element in the `books.xml` file:
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Access the first element's first child (the text node)
var titleTextNode = xmlDoc.getElementsByTagName("title").childNodes;
// Change the text node's value
titleTextNode.nodeValue = "Easy Cooking";
```
### Step-by-Step Explanation
1. **Load the XML:** The `loadXMLDoc()` helper function loads the `books.xml` file into the `xmlDoc` DOM object.
2. **Target the Text Node:** `getElementsByTagName("title")` retrieves the first `` element. `.childNodes` accesses its immediate child, which is the text node containing the actual text.
3. **Update the Value:** Setting `.nodeValue = "Easy Cooking"` replaces the old text with the new string.
---
## Modifying Attribute Values
You can modify the value of an attribute using two different approaches:
1. The `setAttribute()` method (Recommended)
2. The `nodeValue` property of the attribute node
### Method 1: Using `setAttribute()` (Recommended)
The `setAttribute()` method changes the value of an existing attribute. If the attribute does not exist, it will automatically create a new one with the specified name and value.
#### Syntax
```javascript
elementNode.setAttribute(attributeName, attributeValue);
```
#### Code Example
The following example changes the value of the `category` attribute in the first `` element:
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Get the first element
var bookElement = xmlDoc.getElementsByTagName("book");
// Update the "category" attribute to "food"
bookElement.setAttribute("category", "food");
```
#### Step-by-Step Explanation
1. **Load the XML:** Load `books.xml` into the `xmlDoc` object.
2. **Target the Element:** Retrieve the first `` element node.
3. **Set the Attribute:** Call `setAttribute("category", "food")`. If the `category` attribute exists, its value is updated to `"food"`. If it does not exist, a new `category="food"` attribute is added to the element.
---
### Method 2: Using `nodeValue` on Attribute Nodes
Alternatively, you can retrieve the attribute node itself using `getAttributeNode()` and then modify its `nodeValue` property directly.
#### Syntax
```javascript
var attrNode = elementNode.getAttributeNode(attributeName);
attrNode.nodeValue = "new_value";
```
#### Code Example
The following example achieves the same result as Method 1 by targeting the attribute node directly:
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Get the first element
var bookElement = xmlDoc.getElementsByTagName("book");
// Retrieve the "category" attribute node
var categoryAttr = bookElement.getAttributeNode("category");
// Change the attribute node's value
categoryAttr.nodeValue = "food";
```
#### Step-by-Step Explanation
1. **Load the XML:** Load `books.xml` into the `xmlDoc` object.
2. **Target the Element:** Retrieve the first `` element.
3. **Get Attribute Node:** Use `getAttributeNode("category")` to get the actual attribute node object.
4. **Update Value:** Set the `nodeValue` of that attribute node to `"food"`.
---
## Summary & Best Practices
| Task | Recommended Approach | Alternative Approach |
| :--- | :--- | :--- |
| **Change Element Text** | `element.firstChild.nodeValue = "new text"` | `element.childNodes.nodeValue = "new text"` |
| **Change Attribute Value** | `element.setAttribute("attr", "value")` | `element.getAttributeNode("attr").nodeValue = "value"` |
### Key Considerations
* **Whitespace Nodes:** Be careful when using `childNodes` or `firstChild` in XML documents that contain formatting whitespace (newlines and indentation). In some DOM parsers, whitespace between tags is treated as empty text nodes, which can shift the index of your target text node.
* **Attribute Creation:** `setAttribute()` is generally preferred over `nodeValue` for attributes because it automatically handles attribute creation if the attribute does not already exist on the element.
YouTip