Chapter 41: DOM Get Values

XML DOM – Getting Node Values.

I will explain it as if I am your personal teacher sitting next to you — going slowly, using simple analogies, drawing small trees on the whiteboard, showing many different situations, pointing out traps beginners fall into, and giving you copy-paste-ready code you can test immediately.

1. The most important sentence to remember first

There is no single “get value” method that works the same way for every kind of node.

Each type of node has its own meaning for “value”, so you have to choose the right property depending on:

  • whether you are on an element node
  • a text node
  • an attribute node
  • a comment node
  • a CDATA section
  • etc.

2. Quick overview – Which property to use for which node type

Node Type nodeType You usually want… Recommended property / method Returns Typical result example Notes / Traps
Element 1 All readable text inside + descendants textContent string “Atomic HabitsJames Clear499.00” Includes all descendant text + whitespace
Element 1 Only direct text (no child elements) firstChild?.nodeValue?.trim() or data string or null “499.00” May be null if no text child
Text 3 The text itself nodeValue or data string “Atomic Habits” data is alias for nodeValue on text nodes
Attribute 2 The attribute value value or nodeValue string “INR” Both are equivalent
Comment 8 The comment text nodeValue or data string ” Popular book “ Usually ignored in output
CDATASection 4 The raw CDATA content nodeValue or data string not parsed Preserves markup as text
Document 9 null No value
ProcessingInstruction 7 The instruction data nodeValue or data string “version=”1.0″ encoding=”UTF-8″” Rare in practice

Golden rule for beginners

If you are on an element (nodeType 1) and you just want the readable text → use textContent If you are on a text node (nodeType 3) → use nodeValue If you are on an attribute (nodeType 2) → use value or nodeValue

3. Visual example – XML and its nodes with values

XML

Let’s label the nodes and their values:

text

4. Practical code – Getting values from different starting points

JavaScript

5. The most common real-world patterns (copy-paste these)

Pattern 1 – Safe “get text from element” function (most useful)

JavaScript

Pattern 2 – Get direct text only (when you know there are no child elements)

JavaScript

Pattern 3 – Get attribute safely with fallback

JavaScript

Pattern 4 – Get all text values from matching elements

JavaScript

6. Common beginner mistakes & how to fix them

Mistake 1 — Using nodeValue on an element

JavaScript

Fix: Use textContent or firstChild.nodeValue

Mistake 2 — Forgetting to trim whitespace

JavaScript

Fix: Always .trim()

Mistake 3 — Using innerHTML on pure XML DOM

JavaScript

Fix: textContent or nodeValue on text nodes

Mistake 4 — Assuming every element has exactly one text child

XML

→ multiple text nodes + element child

Fix: Use textContent when you want all text

Summary – Quick reference table

Starting node type You probably want… Recommended property Returns Notes / Traps
Element All text inside + descendants textContent string Includes whitespace & all descendants
Element Only direct text firstChild?.nodeValue?.trim() string or null May be null if no text or first child is element
Text The text itself nodeValue or data string Same value
Attribute The attribute value value or nodeValue string Same value
Comment The comment text nodeValue or data string Usually ignored
CDATA The raw CDATA content nodeValue or data string Preserves markup as text

Would you like to continue with one of these next?

  • How to clean & normalize text values (remove extra spaces, collapse newlines)
  • Getting typed values (number, boolean, date) from nodes safely
  • Handling namespaces when getting values (very common in real XML)
  • Real-world patterns — extracting price + currency pairs, collecting all titles, etc.
  • Debugging when nodeValue / textContent gives unexpected results
  • Comparing nodeValue vs textContent vs innerText (differences & pitfalls)

Just tell me which direction you want to go next! 😊

You may also like...

Leave a Reply

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