Chapter 35: Accessing Nodes

XML DOM – Accessing Nodes.

I will explain it as if I am your personal teacher sitting next to you — slowly, clearly, with many small-to-medium examples, visual drawings (in text), real-life analogies, common beginner mistakes, and practical JavaScript code you can copy-paste and run right now.

1. The most important mindset before we start

When you work with XML DOM in JavaScript, you are walking through a tree house.

  • Every tag is a room (Element node)
  • Every piece of text is a note stuck on the wall (Text node)
  • Every attribute is a label on the door (Attribute node)
  • Comments, processing instructions, CDATA sections… are other kinds of objects in the rooms

Accessing nodes means:

  • Finding a specific room (or all rooms that match some description)
  • Entering the room and looking at what’s inside (text, children, attributes)
  • Sometimes changing what’s inside or adding/removing things

Almost everything you do with XML DOM starts with finding nodes.

2. The 5 most important ways to find / access nodes (in order of how often you will use them)

# Method Returns Best used when… Modern / classic? Example line
1 querySelector() First matching Element or null You want one specific element Modern (2011+) xmlDoc.querySelector(“title”)
2 querySelectorAll() NodeList of matching Elements You want all elements that match Modern (2011+) xmlDoc.querySelectorAll(“price”)
3 getElementById() One Element or null You know the id attribute Classic xmlDoc.getElementById(“B101”)
4 getElementsByTagName() HTMLCollection of Elements You want all elements with a certain tag name Classic xmlDoc.getElementsByTagName(“author”)
5 getElementsByClassName() HTMLCollection You are using class attributes (rare in pure XML) Classic (rarely used in XML)

Modern recommendation (2025–2026)

Use querySelector and querySelectorAll for almost everything. They are the most flexible, readable, and powerful methods.

3. Real XML we will work with (copy this for testing)

XML

Save it as library.xml or paste it into a string.

4. Example 1 – Using querySelector (the modern favorite)

JavaScript

5. Example 2 – Using querySelectorAll + looping

JavaScript

6. Example 3 – Accessing attributes & checking existence

JavaScript

7. Example 4 – Walking the tree manually (parent, children, siblings)

JavaScript

8. Common beginner mistakes & how to avoid them

Mistake 1 — Forgetting about text nodes between elements

JavaScript

Fix: Use .children or check nodeType === 1

Mistake 2 — Using innerHTML on XML DOM

JavaScript

Fix: Use textContent or create real elements

Mistake 3 — Assuming querySelector returns text

JavaScript

Fix: price.textContent or price.firstChild.nodeValue

Summary – Quick reference table

You want to… Best method / pattern
Find first element by tag querySelector(“title”)
Find all books querySelectorAll(“book”)
Find by attribute querySelector(‘book[id=”B101″]’)
Find by text content querySelector(‘inStock[text()=”false”]’)
Get all direct element children element.children
Loop only elements (skip text/comments) for (let child of parent.children)
Get text safely element.textContent.trim()
Check if attribute exists element.hasAttribute(“currency”)

Would you like to continue with any of these deeper topics?

  • Ignoring whitespace text nodes (very common need)
  • Accessing & creating CDATA sections
  • Working with namespaces (SOAP, RSS, UBL, GST e-invoice…)
  • Walking the tree manually in detail (firstChild, nextSibling, parentNode…)
  • Real-world patterns (parsing RSS, SOAP response, config file)
  • Debugging when you get null or wrong nodes

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 *