3️⃣ 🧱 XML DOM (Document Object Model)
Estimated reading: 3 minutes 20 views

🧾 XML DOM Node Info – Get Name, Type, Value & Structure Details

🧲 Introduction – Why Learn XML DOM Node Info?

When working with XML documents in the DOM, it’s crucial to know what each node is, what type it represents, and how to extract its name or value. Understanding node information helps you navigate the XML structure precisely, whether you’re filtering elements, checking types, or building data-driven UIs.

🎯 In this guide, you’ll learn:

  • How to get information about XML nodes
  • The most important node properties (nodeName, nodeType, nodeValue)
  • How to use these properties with JavaScript
  • Practical examples for reading XML structure

📄 Sample XML for Reference

<book id="101">
  <title>XML Guide</title>
  <author>Jane Doe</author>
</book>

🔍 Key XML DOM Node Properties

PropertyDescription
nodeNameReturns the name of the node (tag name or “#text”)
nodeTypeReturns an integer indicating the node’s type
nodeValueReturns the value of the node (for text/attribute)

🧩 Node Types Reference Table

Node TypeConstantExample
ELEMENT_NODE1<book>
ATTRIBUTE_NODE2id="101"
TEXT_NODE3Text inside <title>
COMMENT_NODE8<!-- Note -->
DOCUMENT_NODE9The entire XML document

🧪 JavaScript Example – Get Node Info

var xhr = new XMLHttpRequest();
xhr.open("GET", "book.xml", true);
xhr.onreadystatechange = function () {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var xmlDoc = xhr.responseXML;
    var book = xmlDoc.getElementsByTagName("book")[0];

    console.log("Node Name:", book.nodeName);      // "book"
    console.log("Node Type:", book.nodeType);      // 1
    console.log("Node Value:", book.nodeValue);    // null for element
  }
};
xhr.send();

✅ Element nodes return null for nodeValue. Use .textContent for content.


📘 Example – Reading Text Node Info

var titleElement = xmlDoc.getElementsByTagName("title")[0];
var textNode = titleElement.firstChild;

console.log("Node Name:", textNode.nodeName);    // "#text"
console.log("Node Type:", textNode.nodeType);    // 3
console.log("Node Value:", textNode.nodeValue);  // "XML Guide"

✅ Use firstChild.nodeValue to get the inner text of an element.


📦 Accessing Attribute Node Info

var book = xmlDoc.getElementsByTagName("book")[0];
var attr = book.attributes[0];

console.log("Attr Name:", attr.nodeName);    // "id"
console.log("Attr Type:", attr.nodeType);    // 2
console.log("Attr Value:", attr.nodeValue);  // "101"

✅ Attributes are accessed using .attributes[], not directly through the element.


🧰 DOM Methods to Check Node Info

Method / PropertyUse Case
nodeNameIdentify element or “#text” or attribute
nodeTypeFilter specific node types (e.g., text)
nodeValueGet text value or attribute value
parentNodeNavigate to parent node
childNodesGet child nodes (includes text/whitespace)
hasChildNodes()Check if node has any children

✅ Best Practices for Working with Node Info

  • ✔️ Always use nodeType to check if node is 1 (element) or 3 (text)
  • ✔️ Use textContent for reading text easily, unless you need node-level control
  • ✔️ Filter out #text nodes if only elements are needed
  • ❌ Don’t rely on nodeValue for element nodes—it will return null

📌 Summary – Recap & Next Steps

Understanding node info in the XML DOM helps you accurately read, inspect, and manipulate XML documents. Whether you’re analyzing structure or transforming data, nodeName, nodeType, and nodeValue are the essential tools.

🔍 Key Takeaways:

  • nodeName, nodeType, and nodeValue describe any XML node
  • Use .attributes[] to access and inspect attributes
  • Text is always in a child node with nodeType === 3

⚙️ Real-world relevance: Essential for XML validators, document editors, config UIs, and any app that dynamically inspects XML documents.


❓ FAQs – XML DOM Node Info

❓ What does nodeName return for an element?
✅ The tag name (e.g., "book").

❓ What does nodeName return for a text node?
"#text".

❓ Why is nodeValue null for elements?
✅ Only text and attribute nodes have a nodeValue; elements do not.

❓ How do I get the name and value of an attribute?
✅ Use .attributes[0].nodeName and .attributes[0].nodeValue.

❓ Can I use textContent instead of nodeValue?
✅ Yes. textContent is simpler and more modern.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XML DOM Node Info

Or Copy Link

CONTENTS
Scroll to Top