XML DOM Advanced
Advanced XML DOM Manipulation
Section titled “Advanced XML DOM Manipulation”The XML Document Object Model (DOM) provides a standard way to access and manipulate the structure and content of XML documents. While basic traversal was introduced earlier, this section covers more advanced techniques for reading and modifying XML data using DOM APIs, typically within a JavaScript environment.
Note: The examples use standard DOM methods common in web browsers. Modern JavaScript offers convenient alternatives like querySelector and querySelectorAll which accept CSS selectors (and sometimes basic XPath) for node selection, often leading to more concise code.
Assume xmlDoc is a variable holding a parsed XML DOM object (e.g., loaded via fetch and DOMParser).
Get Element Content (Text Value)
Section titled “Get Element Content (Text Value)”To retrieve the text content inside an element:
Using specific DOM traversal (less common now):
// Get the first <title> elementconst firstTitleElement = xmlDoc.getElementsByTagName("title")[0];// Access its first child node (which should be the text node)const textNode = firstTitleElement.childNodes[0];// Get the value of the text nodeconst titleText = textNode.nodeValue;Using the simpler .textContent property (Recommended):
// Get the text content of the first <title> element directlyconst titleText = xmlDoc.getElementsByTagName("title")[0].textContent;Explanation: .textContent conveniently concatenates the text content of all child nodes, which is usually what’s desired. The childNodes[0].nodeValue approach is more explicit but brittle if the element contains comments or other node types before the text.
Get Attribute Value
Section titled “Get Attribute Value”To retrieve the value of an attribute associated with an element:
// Get the first <title> elementconst firstTitleElement = xmlDoc.getElementsByTagName("title")[0];// Get the value of its 'lang' attributeconst langValue = firstTitleElement.getAttribute("lang"); // Returns "en" in the exampleIf the attribute doesn’t exist, getAttribute() returns null.
Change Element Content (Text Value)
Section titled “Change Element Content (Text Value)”To modify the text content of an element:
Using specific DOM manipulation (less common):
// Get the first <title> element's text nodeconst textNode = xmlDoc.getElementsByTagName("title")[0].childNodes[0];// Change its valuetextNode.nodeValue = "Easy XML Cooking";Using the simpler .textContent property (Recommended):
// Get the first <title> elementconst firstTitleElement = xmlDoc.getElementsByTagName("title")[0];// Change its text contentfirstTitleElement.textContent = "Easy XML Cooking";Setting .textContent replaces all existing child nodes of the element with a single new text node.
Add or Modify an Attribute
Section titled “Add or Modify an Attribute”The setAttribute() method adds a new attribute or changes the value of an existing one.
// Get all <book> elementsconst books = xmlDoc.getElementsByTagName("book");
// Add/update an 'edition' attribute for each bookfor (let i = 0; i < books.length; i++) { books[i].setAttribute("edition", "first");}If the ‘edition’ attribute already exists, its value will be updated to ‘first’. If it doesn’t exist, it will be created.
Create and Add a New Element
Section titled “Create and Add a New Element”To add a new element (e.g., <edition>First</edition>) to an existing element:
- Use
createElement()to create the new element node (<edition>). - Use
createTextNode()to create the text content (First). (Alternatively, set.textContentafter creating the element). - Use
appendChild()to add the text node as a child of the new element node. - Use
appendChild()again to add the new element (with its text child) to the desired parent element (e.g., the first<book>).
Example:
// 1. Create the new <edition> elementconst newElement = xmlDoc.createElement("edition");
// 2. Create the text contentconst newText = xmlDoc.createTextNode("First");
// 3. Append text to the new elementnewElement.appendChild(newText);
// --- OR using textContent (simpler) ---// const newElement = xmlDoc.createElement("edition");// newElement.textContent = "First";// -------------------------------------
// 4. Get the target parent element (first <book>)const firstBook = xmlDoc.getElementsByTagName("book")[0];
// 5. Append the new <edition> element to the first <book>firstBook.appendChild(newElement);Remove an Element or Node
Section titled “Remove an Element or Node”To remove a node (element, text node, comment, etc.), you need a reference to the node itself and its parent node. Then use the parent’s removeChild() method.
Example: Remove the first child node (could be text, an element, etc.) from the first <book> element:
// Get the first <book> elementconst firstBook = xmlDoc.getElementsByTagName("book")[0];
// Check if it has any child nodesif (firstBook.hasChildNodes()) { // Get the first child node const nodeToRemove = firstBook.childNodes[0]; // Remove it using the parent firstBook.removeChild(nodeToRemove);}Important Note on childNodes: The childNodes collection includes all node types, including elements, text nodes (even those containing only whitespace), and comments. This can sometimes lead to unexpected results if you assume childNodes[0] is always the first element child. Be mindful of whitespace nodes between elements which parsers might create. If you only want element children, consider using the .children property or iterating childNodes and checking nodeType.
Exploring the full XML DOM specification (e.g., on MDN Web Docs) reveals many more methods for detailed navigation and manipulation.