Chapter 34: DOM Nodes

XML DOM Nodes, written as if I am your patient teacher sitting next to you — drawing on the whiteboard, showing examples, making comparisons, pointing out common confusion, and giving you plenty of practical code you can try right away.

We will go very slowly and clearly so that you really understand:

  • What exactly a “node” is
  • The different types of nodes you will meet in XML
  • How they are organized in a tree
  • How to recognize and work with each type in JavaScript
  • Real examples with typical patterns and mistakes

Let’s start from the beginning.

1. What is a “Node” in the XML DOM?

When the browser (or any XML parser) reads your XML document, it does not keep it as one big string. It breaks the document into small pieces called nodes.

A node is the smallest unit the DOM works with.

Think of the XML document as a big Lego house. The DOM is the moment when someone takes the house apart into individual Lego bricks — each brick is a node.

Every single thing in the XML document becomes one of these bricks:

  • Every opening tag
  • Every closing tag
  • Every piece of text
  • Every attribute
  • Every comment
  • Even the spaces between tags (sometimes!)

These bricks (nodes) are then connected together in a tree structure — parents have children, siblings are next to each other, etc.

2. The 7 main types of nodes you will meet in XML DOM

Node Type Number Node Type Name What it represents Common JavaScript way to check Real example in XML
1 Element Any tag: <book>, <name>, <price>… node.nodeType === 1 <book>, <price currency=”INR”>
2 Attribute Attributes inside opening tags node.nodeType === 2 id=”B101″, currency=”INR”
3 Text Plain text content between tags node.nodeType === 3 Atomic Habits, 499.00
4 CDATASection Text that should not be parsed as markup node.nodeType === 4 <![CDATA[<b>not a tag</b>]]>
7 ProcessingInstruction <?xml … ?> or <?php … ?> node.nodeType === 7 <?xml version=”1.0″ encoding=”UTF-8″?>
8 Comment <!– comment –> node.nodeType === 8 <!– this is a note –>
9 Document The entire document (the root container) node.nodeType === 9 The whole XML document

The two types you will use 95% of the time:

  • Element nodes (type 1)
  • Text nodes (type 3)

3. Visual example – Let’s see the nodes in a real XML

Take this small but realistic XML:

XML

Here is how the DOM sees it (node by node):

text

4. Practical JavaScript – How to recognize and work with each node type

JavaScript

You will see output similar to:

text

5. Common beginner mistakes with nodes

  1. Forgetting that whitespace creates Text nodes
XML

→ There are Text nodes containing whitespace between <book> and <title> and after </title>

  1. Using innerHTML on XML nodes
JavaScript

Correct: use textContent or create real elements.

  1. Assuming all children are elements
JavaScript

Correct way:

JavaScript

6. Quick Summary – XML DOM Nodes Cheat Sheet

Node type nodeType How to check Most common use case Typical code example
Element 1 node.nodeType === 1 Tags, containers document.querySelector(“book”)
Attribute 2 node.nodeType === 2 Reading id, class, currency… element.getAttribute(“id”)
Text 3 node.nodeType === 3 Getting/setting text content element.textContent = “New value”
CDATA 4 node.nodeType === 4 Raw code, HTML snippets inside XML <![CDATA[<b>not parsed</b>]]>
Comment 8 node.nodeType === 8 Developer notes (usually ignored) <!– todo: update price –>
Document 9 node.nodeType === 9 Root of everything xmlDoc.documentElement

Would you like to continue with one of these next?

  • How to ignore whitespace text nodes (very common need)
  • Walking the tree manually (parentNode, firstChild, nextSibling…)
  • Creating elements & attributes from scratch
  • Handling namespaces in real XML (SOAP, RSS, e-invoice…)
  • Common real-world patterns (RSS parsing, SOAP response, config file)
  • Debugging tricks when responseXML is null

Just tell me which direction feels most useful for you right now! 😊

You may also like...

Leave a Reply

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