YouTip LogoYouTip

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 <title> 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 `<title>` 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 `<book>` element: ```javascript // Load the XML document var xmlDoc = loadXMLDoc("books.xml"); // Get the first <book> 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 `<book>` 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 <book> 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 `<book>` 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.</div><div class="post-nav"><a href="/post/dom-nodes-remove.html">← Dom Nodes Remove</a><a href="/post/dom-nodes-get.html">Dom Nodes Get β†’</a></div></div> <div class="sidebar"><div class="sidebar-widget"><h3>πŸ“‚ Categories</h3><ul><li><a href="/category/18.html">⚑ JavaScript</a> <span style="color:#999;font-size:12px">(1589)</span></li><li><a href="/category/23.html">🐘 PHP</a> <span style="color:#999;font-size:12px">(872)</span></li><li><a href="/category/27.html">🐍 Python3</a> <span style="color:#999;font-size:12px">(810)</span></li><li><a href="/category/15.html">🌐 HTML</a> <span style="color:#999;font-size:12px">(691)</span></li><li><a href="/category/31.html">βš™οΈ C#</a> <span style="color:#999;font-size:12px">(650)</span></li><li><a href="/category/26.html">🐍 Python</a> <span style="color:#999;font-size:12px">(594)</span></li><li><a href="/category/28.html">β˜• Java</a> <span style="color:#999;font-size:12px">(552)</span></li><li><a href="/category/72.html">βš™οΈ PyTorch</a> <span style="color:#999;font-size:12px">(534)</span></li><li><a href="/category/75.html">🐧 Linux</a> <span style="color:#999;font-size:12px">(472)</span></li><li><a href="/category/29.html">βš™οΈ C</a> <span style="color:#999;font-size:12px">(432)</span></li><li><a href="/category/20.html">πŸ“¦ jQuery</a> <span style="color:#999;font-size:12px">(406)</span></li><li><a href="/category/16.html">🎨 CSS</a> <span style="color:#999;font-size:12px">(377)</span></li><li><a href="/category/64.html">πŸ“„ XML</a> <span style="color:#999;font-size:12px">(259)</span></li><li><a href="/category/59.html">πŸ“¦ jQuery UI</a> <span style="color:#999;font-size:12px">(231)</span></li><li><a href="/category/60.html">🎯 Bootstrap</a> <span style="color:#999;font-size:12px">(220)</span></li><li><a href="/category/30.html">βš™οΈ C++</a> <span style="color:#999;font-size:12px">(215)</span></li><li><a href="/category/58.html">πŸ…°οΈ Angular</a> <span style="color:#999;font-size:12px">(205)</span></li><li><a href="/category/19.html">🌐 HTML DOM</a> <span style="color:#999;font-size:12px">(201)</span></li><li><a href="/category/50.html">πŸ”΄ Redis</a> <span style="color:#999;font-size:12px">(188)</span></li><li><a href="/category/90.html">πŸ“– Web Building</a> <span style="color:#999;font-size:12px">(142)</span></li><li><a href="/category/57.html">πŸ’š Vue.js</a> <span style="color:#999;font-size:12px">(141)</span></li><li><a href="/category/43.html">πŸ“ˆ R</a> <span style="color:#999;font-size:12px">(131)</span></li><li><a href="/category/69.html">🐼 Pandas</a> <span style="color:#999;font-size:12px">(124)</span></li><li><a href="/category/24.html">πŸ—„οΈ SQL</a> <span style="color:#999;font-size:12px">(105)</span></li><li><a href="/category/76.html">βš™οΈ Docker</a> <span style="color:#999;font-size:12px">(86)</span></li><li><a href="/category/36.html">βš™οΈ TypeScript</a> <span style="color:#999;font-size:12px">(73)</span></li><li><a href="/category/85.html">βš™οΈ Highcharts</a> <span style="color:#999;font-size:12px">(70)</span></li><li><a href="/category/95.html">πŸ“– AI Agent</a> <span style="color:#999;font-size:12px">(70)</span></li><li><a href="/category/56.html">βš™οΈ React</a> <span style="color:#999;font-size:12px">(68)</span></li><li><a href="/category/35.html">πŸ“– Node.js</a> <span style="color:#999;font-size:12px">(65)</span></li><li><a href="/category/94.html">βš™οΈ Machine Learning</a> <span style="color:#999;font-size:12px">(60)</span></li><li><a href="/category/77.html">πŸ“ Git</a> <span style="color:#999;font-size:12px">(59)</span></li><li><a href="/category/32.html">πŸ”΅ Go</a> <span style="color:#999;font-size:12px">(58)</span></li><li><a href="/category/83.html">πŸ“ˆ Markdown</a> <span style="color:#999;font-size:12px">(58)</span></li><li><a href="/category/68.html">πŸ”’ NumPy</a> <span style="color:#999;font-size:12px">(55)</span></li><li><a href="/category/66.html">πŸ§ͺ Flask</a> <span style="color:#999;font-size:12px">(54)</span></li><li><a href="/category/41.html">βš™οΈ Scala</a> <span style="color:#999;font-size:12px">(53)</span></li><li><a href="/category/48.html">πŸ—„οΈ SQLite</a> <span style="color:#999;font-size:12px">(52)</span></li><li><a href="/category/116.html">πŸ“– JSTL</a> <span style="color:#999;font-size:12px">(52)</span></li><li><a href="/category/106.html">βš™οΈ VS Code</a> <span style="color:#999;font-size:12px">(51)</span></li><li><a href="/category/49.html">πŸƒ MongoDB</a> <span style="color:#999;font-size:12px">(49)</span></li><li><a href="/category/42.html">πŸ“ˆ Perl</a> <span style="color:#999;font-size:12px">(48)</span></li><li><a href="/category/34.html">πŸ“ˆ Ruby</a> <span style="color:#999;font-size:12px">(47)</span></li><li><a href="/category/70.html">πŸ“Š Matplotlib</a> <span style="color:#999;font-size:12px">(47)</span></li><li><a href="/category/1.html">βš™οΈ Uncategorized</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/37.html">🍎 Swift</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/51.html">πŸ—„οΈ PostgreSQL</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/88.html">βš™οΈ Data Structures</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/100.html">πŸ“ˆ Playwright</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/54.html">πŸ“– iOS</a> <span style="color:#999;font-size:12px">(45)</span></li><li><a href="/category/25.html">πŸ—„οΈ MySQL</a> <span style="color:#999;font-size:12px">(44)</span></li><li><a href="/category/108.html">βš™οΈ LangChain</a> <span style="color:#999;font-size:12px">(43)</span></li><li><a href="/category/67.html">πŸ“– FastAPI</a> <span style="color:#999;font-size:12px">(40)</span></li><li><a href="/category/112.html">βš™οΈ Ionic</a> <span style="color:#999;font-size:12px">(38)</span></li><li><a href="/category/87.html">πŸ“ˆ Design Patterns</a> <span style="color:#999;font-size:12px">(37)</span></li><li><a href="/category/114.html">βš™οΈ Eclipse</a> <span style="color:#999;font-size:12px">(37)</span></li><li><a href="/category/17.html">🎨 CSS3</a> <span style="color:#999;font-size:12px">(34)</span></li><li><a href="/category/40.html">πŸŒ™ Lua</a> <span style="color:#999;font-size:12px">(34)</span></li><li><a href="/category/127.html">βš™οΈ Codex</a> <span style="color:#999;font-size:12px">(34)</span></li><li><a href="/category/65.html">🎸 Django</a> <span style="color:#999;font-size:12px">(32)</span></li><li><a href="/category/102.html">βš™οΈ OpenCV</a> <span style="color:#999;font-size:12px">(32)</span></li><li><a href="/category/33.html">πŸ“ˆ Rust</a> <span style="color:#999;font-size:12px">(31)</span></li><li><a href="/category/117.html">πŸ“– JSP</a> <span style="color:#999;font-size:12px">(31)</span></li><li><a href="/category/126.html">βš™οΈ Claude Code</a> <span style="color:#999;font-size:12px">(31)</span></li><li><a href="/category/101.html">πŸ“– Pillow</a> <span style="color:#999;font-size:12px">(30)</span></li><li><a href="/category/130.html">βš™οΈ OpenCode</a> <span style="color:#999;font-size:12px">(28)</span></li><li><a href="/category/131.html">πŸ“– AI Skills</a> <span style="color:#999;font-size:12px">(27)</span></li><li><a href="/category/55.html">πŸ“ˆ Flutter</a> <span style="color:#999;font-size:12px">(26)</span></li><li><a href="/category/80.html">πŸ“– Maven</a> <span style="color:#999;font-size:12px">(26)</span></li><li><a href="/category/61.html">🎨 Tailwind CSS</a> <span style="color:#999;font-size:12px">(25)</span></li><li><a href="/category/73.html">🧠 TensorFlow</a> <span style="color:#999;font-size:12px">(25)</span></li><li><a href="/category/118.html">πŸ“ˆ Servlet</a> <span style="color:#999;font-size:12px">(24)</span></li><li><a href="/category/39.html">πŸ“ˆ Dart</a> <span style="color:#999;font-size:12px">(23)</span></li><li><a href="/category/46.html">πŸ“– Assembly</a> <span style="color:#999;font-size:12px">(23)</span></li><li><a href="/category/52.html">βš™οΈ Memcached</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/63.html">✏️ SVG</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/98.html">βš™οΈ Electron</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/103.html">πŸ“– NLP</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/82.html">πŸ” Regex</a> <span style="color:#999;font-size:12px">(21)</span></li><li><a href="/category/53.html">πŸ“ˆ Android</a> <span style="color:#999;font-size:12px">(20)</span></li><li><a href="/category/38.html">🟣 Kotlin</a> <span style="color:#999;font-size:12px">(19)</span></li><li><a href="/category/44.html">πŸ“– Julia</a> <span style="color:#999;font-size:12px">(19)</span></li><li><a href="/category/92.html">πŸ“– SOAP</a> <span style="color:#999;font-size:12px">(17)</span></li><li><a href="/category/99.html">πŸ“– Selenium</a> <span style="color:#999;font-size:12px">(17)</span></li><li><a href="/category/104.html">πŸ“ˆ PowerShell</a> <span style="color:#999;font-size:12px">(17)</span></li><li><a href="/category/62.html">πŸ’… Sass</a> <span style="color:#999;font-size:12px">(16)</span></li><li><a href="/category/89.html">πŸ“– HTTP</a> <span style="color:#999;font-size:12px">(16)</span></li><li><a href="/category/45.html">πŸ“– Zig</a> <span style="color:#999;font-size:12px">(15)</span></li><li><a href="/category/113.html">πŸ“– AI</a> <span style="color:#999;font-size:12px">(15)</span></li><li><a href="/category/21.html">πŸ”„ AJAX</a> <span style="color:#999;font-size:12px">(14)</span></li><li><a href="/category/110.html">πŸ“ˆ Swagger</a> <span style="color:#999;font-size:12px">(14)</span></li><li><a href="/category/74.html">βš™οΈ Scikit-learn</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/84.html">βš™οΈ ECharts</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/86.html">βš™οΈ Chart.js</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/128.html">βš™οΈ Cursor</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/71.html">βš™οΈ SciPy</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/91.html">πŸ“ˆ RDF</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/96.html">πŸ“– Ollama</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/97.html">πŸ“– Next.js</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/129.html">πŸ“– Plotly Dash</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/22.html">πŸ“‹ JSON</a> <span style="color:#999;font-size:12px">(11)</span></li><li><a href="/category/109.html">πŸ“ˆ RESTful API</a> <span style="color:#999;font-size:12px">(11)</span></li><li><a href="/category/93.html">πŸ“– WSDL</a> <span style="color:#999;font-size:12px">(9)</span></li><li><a href="/category/81.html">βš™οΈ CMake</a> <span style="color:#999;font-size:12px">(8)</span></li><li><a href="/category/115.html">πŸ“ˆ Firebug</a> <span style="color:#999;font-size:12px">(7)</span></li><li><a href="/category/78.html">πŸ“– Nginx</a> <span style="color:#999;font-size:12px">(6)</span></li><li><a href="/category/79.html">☸️ Kubernetes</a> <span style="color:#999;font-size:12px">(6)</span></li><li><a href="/category/107.html">πŸ“ˆ Jupyter</a> <span style="color:#999;font-size:12px">(6)</span></li><li><a href="/category/105.html">πŸ“– LaTeX</a> <span style="color:#999;font-size:12px">(4)</span></li><li><a href="/category/111.html">πŸ“– UniApp</a> <span style="color:#999;font-size:12px">(4)</span></li><li><a href="/category/47.html">πŸ—„οΈ SQL Server</a> <span style="color:#999;font-size:12px">(1)</span></li></ul></div></div> </div> </div> <footer> <div class="container"> <p>YouTip © 2024-2026 | <a href="/">Home</a> | Learn Technology, Build Dreams!</p> <p>All content is for educational and learning purposes only.</p> </div> </footer> <!-- Global Analytics Tracker --> <script> // Placeholder for Google Analytics </script> </body> </html>