DOM Nodes and Node Types
Estimated study time: 14 minutes.
Everything in an XML DOM tree — elements, text, attributes, comments — is represented as a node. Different kinds of content are represented by different node types.
Common Node Types
- Element Node (type 1) — represents a tag, like
<title>. - Attribute Node (type 2) — represents an attribute, like
id="102". - Text Node (type 3) — represents the actual text content inside an element.
- Comment Node (type 8) — represents an XML comment.
- Document Node (type 9) — the root object representing the entire document.
Example Breakdown
<course id="DA-101">
<!-- main course info -->
<title>Data Analytics</title>
</course>
courseis an element node.id="DA-101"is an attribute node oncourse.- The comment is a comment node.
titleis an element node containing a single text node: "Data Analytics".
Why This Matters
Knowing node types matters because DOM methods often behave differently, or only apply to, specific node types — for example, only element nodes have attributes, and only text nodes hold raw character data directly.
💡 Tip: When debugging unexpected DOM output, check the node type first — a common bug is treating a text node as if it were the element that contains it.