DOM Methods and Properties
Estimated study time: 15 minutes.
Once an XML document is loaded into a DOM tree, a standard set of methods and properties lets you read and modify it programmatically.
Common Properties
node.nodeName // the tag name, e.g. "title"
node.nodeValue // the text value, for text nodes
node.nodeType // numeric code for the node type
node.parentNode // the parent node
node.childNodes // list of direct child nodes
node.attributes // list of attributes on this node
Common Methods
doc.getElementsByTagName("title") // all elements with this tag name
node.getAttribute("id") // read a specific attribute's value
node.setAttribute("id", "DA-102") // set or update an attribute
node.appendChild(newNode) // add a new child node
node.removeChild(oldNode) // remove a child node
doc.createElement("module") // create a brand-new element node
Example (JavaScript)
let titleNode = xmlDoc.getElementsByTagName("title")[0];
console.log(titleNode.textContent); // "Data Analytics"
titleNode.textContent = "Advanced Data Analytics";
Reading vs Modifying
Properties like nodeValue and methods like getAttribute are read operations. Methods like setAttribute, appendChild, and removeChild mutate the tree — after changes, you'd typically serialize the DOM back into an XML string to save it.
💡 Tip: After modifying a DOM tree, always re-serialize and re-validate the result — it's easy to accidentally create malformed XML through methods like
appendChild.