🧾 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
Property | Description |
---|---|
nodeName | Returns the name of the node (tag name or “#text”) |
nodeType | Returns an integer indicating the node’s type |
nodeValue | Returns the value of the node (for text/attribute) |
🧩 Node Types Reference Table
Node Type | Constant | Example |
---|---|---|
ELEMENT_NODE | 1 | <book> |
ATTRIBUTE_NODE | 2 | id="101" |
TEXT_NODE | 3 | Text inside <title> |
COMMENT_NODE | 8 | <!-- Note --> |
DOCUMENT_NODE | 9 | The 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 / Property | Use Case |
---|---|
nodeName | Identify element or “#text” or attribute |
nodeType | Filter specific node types (e.g., text) |
nodeValue | Get text value or attribute value |
parentNode | Navigate to parent node |
childNodes | Get 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 is1
(element) or3
(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 returnnull
📌 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
, andnodeValue
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 :