TExcelTAD
Home › XML › DOM Nodes

XML DOM Nodes

In the DOM, everything is a node. The document itself, every element, every attribute, every piece of text — even the line breaks between elements — becomes an object in the tree. Knowing which kinds of nodes exist explains a lot of otherwise surprising behaviour.

Node types

Node nodeType Constant Example
Element 1 Node.ELEMENT_NODE <book>
Attribute 2 Node.ATTRIBUTE_NODE id="b1"
Text 3 Node.TEXT_NODE The Hobbit
CDATA section 4 Node.CDATA_SECTION_NODE <![CDATA[ … ]]>
Processing instruction 7 Node.PROCESSING_INSTRUCTION_NODE <?page-break?>
Comment 8 Node.COMMENT_NODE <!-- note -->
Document 9 Node.DOCUMENT_NODE the whole document
Document type 10 Node.DOCUMENT_TYPE_NODE <!DOCTYPE …>

The tree for a small document

xml
<book id="b1">
  <title>The Hobbit</title>
</book>
text
Document (9)
└── Element book (1)          ← attribute id="b1" (2) hangs off the element
    ├── Text "\n  " (3)        ← white space!
    ├── Element title (1)
    │   └── Text "The Hobbit" (3)
    └── Text "\n" (3)          ← white space!

Notice two things:

  1. Text lives in its own node. The title element doesn't have a value; it has a child text node that holds The Hobbit.
  2. White space is text. The newline and indentation between <book> and <title> become text nodes. book therefore has three child nodes, not one.

Let's prove it:

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<book id="b1">
  <title>The Hobbit</title>
</book>`;
    const doc = new DOMParser().parseFromString(xml, 'application/xml');
    const book = doc.documentElement;

    console.log('book.childNodes.length =', book.childNodes.length);
    for (const n of book.childNodes) {
      console.log(' ', n.nodeType, n.nodeName, JSON.stringify(n.nodeValue));
    }
    console.log('book.children.length   =', book.children.length, '(elements only)');

    const title = book.children[0];
    console.log('title text node:', title.firstChild.nodeType, JSON.stringify(title.firstChild.nodeValue));
    console.log('attribute node :', book.getAttributeNode('id').nodeType, book.getAttributeNode('id').value);
  </script>
</body>
</html>

Attributes are special

Attribute nodes are not children of their element — they don't appear in childNodes and have no parentNode. You reach them through the element's attributes collection or getAttributeNode(); usually getAttribute() (which returns a plain string) is all you need.

Seeing every node

This helper walks the whole tree and prints each node with indentation, making the invisible text nodes visible:

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<?xml-stylesheet href="x.css"?>
<library>
  <!-- two books -->
  <book id="b1"><title>The Hobbit</title></book>
  <book id="b2"><title><![CDATA[Tom & Jerry]]></title></book>
</library>`;
    const doc = new DOMParser().parseFromString(xml, 'application/xml');
    const NAMES = { 1: 'ELEMENT', 3: 'TEXT', 4: 'CDATA', 7: 'PI', 8: 'COMMENT', 9: 'DOCUMENT' };

    function dump(node, depth) {
      const label = NAMES[node.nodeType] || node.nodeType;
      const value = node.nodeValue === null ? '' : ' ' + JSON.stringify(node.nodeValue);
      const attrs = node.attributes ? [...node.attributes].map(a => ` @${a.name}=${a.value}`).join('') : '';
      console.log('  '.repeat(depth) + label + ' ' + node.nodeName + attrs + value);
      for (const child of node.childNodes) dump(child, depth + 1);
    }
    dump(doc, 0);
  </script>
</body>
</html>

Tip: When you only care about elements, use the element-only properties: children, firstElementChild, lastElementChild, nextElementSibling, previousElementSibling. They skip text and comment nodes for you.

Node relationships

Every node (except the document) has exactly one parent. Nodes with the same parent are siblings. The DOM exposes these as properties, which the Navigating lesson covers in depth:

Property Points to
parentNode The parent
childNodes All children (live list)
firstChild / lastChild First / last child of any type
nextSibling / previousSibling Neighbouring nodes of any type

Test yourself

What is the nodeType of a text node?

  1. 1
  2. 2
  3. 3
  4. 9

How many child **nodes** does <a>\n <b/>\n</a> have?

  1. 1
  2. 2
  3. 3
  4. 0

Are attribute nodes included in an element's childNodes?

  1. Yes, always first
  2. Yes, always last
  3. No — they are reached via attributes or getAttribute
  4. Only in XML, not HTML