Chapter 45: DOM Create Nodes

XML DOM – Creating Nodes.

I will explain it as if I’m your personal teacher sitting next to you — going slowly, using simple analogies, drawing small trees on the whiteboard, showing before → after examples, warning about every possible trap, and giving you realistic, copy-paste-ready code you can run right now.

1. What does “creating nodes” actually mean?

Creating a node = making a new piece (element, text, attribute, comment, CDATA, etc.) that didn’t exist before.

Once created, you usually want to attach it somewhere in the tree (append, insert before/after, replace, etc.).

Key facts you must remember from the first minute:

  • All new nodes are created by the document that will own them → document.createElement(), document.createTextNode(), document.createAttribute(), etc. → You cannot create a node using a different document and then move it (you’ll get WrongDocumentError)
  • Created nodes are not automatically in the tree → They float in memory until you attach them with appendChild(), insertBefore(), etc.
  • You can create standalone nodes, work on them, set attributes, add children, and only attach them at the end

Analogy

Imagine the DOM is a big Lego city already built.

  • document.createElement(“house”) = you just bought a new Lego house kit
  • You can paint it, add windows, put furniture inside while it’s still in your hands
  • Only when you decide to glue it into the city (attach it to a street/parent) does it become part of the real city

2. The most important creation methods

What you want to create Method Returns Example line Most common use case
New element document.createElement(“tagname”) Element const book = xmlDoc.createElement(“book”) Almost everything — books, items, sections
New text node document.createTextNode(“text”) Text const txt = xmlDoc.createTextNode(“Hello world”) Adding readable content inside elements
New attribute document.createAttribute(“name”) Attr const attr = xmlDoc.createAttribute(“currency”) Rare — usually use setAttribute() instead
New comment document.createComment(“text”) Comment const note = xmlDoc.createComment(“TODO: update”) Developer notes (usually ignored)
New CDATA section document.createCDATASection(“text”) CDATASection const cdata = xmlDoc.createCDATASection(“<b>raw</b>”) When you want raw HTML/JS inside XML
New processing instruction document.createProcessingInstruction(target, data) ProcessingInstruction const pi = xmlDoc.createProcessingInstruction(“xml”, “version=\”1.0\””) Rare — usually only at document start

Modern best practice (2025–2026)

For 99% of cases you only need:

  • createElement()
  • createTextNode()
  • setAttribute() (much easier than createAttribute)

3. Visual before / after – Creating & attaching nodes

Before (original XML)

XML

We want to:

  • Add a new <author> element
  • Add a new <price> element with attribute
  • Add a comment before the new author

After

XML

4. Complete working example – Creating different kinds of nodes

HTML

Summary – Quick reference for creating nodes

What you want to create Method Then attach with… Most common pattern
New element doc.createElement(“discount”) parent.appendChild() or insertBefore() Create → set text → set attributes → attach
New text content doc.createTextNode(“text”) element.appendChild() Usually use element.textContent = “…” instead
New attribute element.setAttribute(“name”, “value”) — (no need to attach) Almost always use setAttribute
New comment doc.createComment(“note”) parent.insertBefore() Rare — mostly for documentation
New CDATA section doc.createCDATASection(“raw text”) parent.appendChild() When you want raw HTML/JS inside XML

Would you like to continue with one of these next?

  • Inserting at specific positions (before/after certain nodes)
  • Creating nodes from scratch and building whole subtrees
  • Moving nodes (cut from one place → paste in another)
  • Replacing while creating (create new → replace old)
  • Real-world patterns — adding new products, adding metadata, building dynamic catalogs
  • Debugging when new nodes “don’t appear” or XML becomes invalid

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

You may also like...

Leave a Reply

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