YouTip LogoYouTip

Dom Nodetype

# DOM Node Types: A Comprehensive Reference In the Document Object Model (DOM), an XML or HTML document is represented as a hierarchical tree of nodes. Every single part of a documentβ€”including the document itself, elements, attributes, text, and commentsβ€”is a node. Understanding the different **Node Types** is essential for traversing, manipulating, and querying the DOM tree programmatically using JavaScript. --- ## Introduction to DOM Nodes The DOM represents a document as a tree structure where each branch ends in a node, and each node contains objects. The `nodeType` property is a read-only property of the `Node` interface that returns an unsigned short integer representing the type of the node. ### The `books.xml` Dataset The examples in this tutorial refer to a standard XML file (`books.xml`) structured as follows: ```xml Everyday Italian Giada De Laurentiis 2005 30.00 ``` --- ## Node Types and Child Nodes The table below lists the 12 standard W3C node types, their descriptions, and the types of child nodes they are permitted to contain: | Node Type | Description | Permitted Child Nodes | | :--- | :--- | :--- | | **Document** | Represents the entire document (the root of the DOM tree). | `Element` (maximum of one), `ProcessingInstruction`, `Comment`, `DocumentType` | | **DocumentFragment** | Represents a "lightweight" or minimal Document object, often used to temporarily store a portion of a document structure. | `Element`, `ProcessingInstruction`, `Comment`, `Text`, `CDATASection`, `EntityReference` | | **DocumentType** | Provides an interface to the entities defined for the document (the `` declaration). | None | | **ProcessingInstruction** | Represents a processing instruction (e.g., ``). | None | | **EntityReference** | Represents an entity reference (e.g., `&`). | `Element`, `ProcessingInstruction`, `Comment`, `Text`, `CDATASection`, `EntityReference` | | **Element** | Represents an element node (e.g., `` or `
`). | `Element`, `Text`, `Comment`, `ProcessingInstruction`, `CDATASection`, `EntityReference` | | **Attr** | Represents an attribute of an element (e.g., `category="cooking"`). | `Text`, `EntityReference` | | **Text** | Represents the textual content inside an element or attribute. | None | | **CDATASection** | Represents a CDATA section in XML (text that will not be parsed by the XML parser). | None | | **Comment** | Represents a comment (e.g., ``). | None | | **Entity** | Represents an entity. | `Element`, `ProcessingInstruction`, `Comment`, `Text`, `CDATASection`, `EntityReference` | | **Notation** | Represents a notation declared in the DTD. | None | --- ## Node Properties: `nodeName` and `nodeValue` Depending on the node type, the `nodeName` and `nodeValue` properties return different values. The table below outlines what each node type returns: | Node Type | Returned `nodeName` | Returned `nodeValue` | | :--- | :--- | :--- | | **Document** | `#document` | `null` | | **DocumentFragment** | `#document-fragment` | `null` | | **DocumentType** | Document type name (e.g., `html`) | `null` | | **EntityReference** | Name of the entity reference | `null` | | **Element** | Element tag name (uppercase in HTML, case-sensitive in XML) | `null` | | **Attr** | Attribute name | Attribute value | | **ProcessingInstruction** | Target of the instruction | Entire content of the node | | **Comment** | `#comment` | Comment text content | | **Text** | `#text` | Text content of the node | | **CDATASection** | `#cdata-section` | Text content of the CDATA section | | **Entity** | Entity name | `null` | | **Notation** | Notation name | `null` | --- ## Node Type Named Constants Instead of remembering numeric codes, you can use named constants defined on the `Node` interface. The table below maps the numeric values to their corresponding named constants: | Numeric Value | Named Constant | | :--- | :--- | | **1** | `Node.ELEMENT_NODE` | | **2** | `Node.ATTRIBUTE_NODE` | | **3** | `Node.TEXT_NODE` | | **4** | `Node.CDATA_SECTION_NODE` | | **5** | `Node.ENTITY_REFERENCE_NODE` | | **6** | `Node.ENTITY_NODE` | | **7** | `Node.PROCESSING_INSTRUCTION_NODE` | | **8** | `Node.COMMENT_NODE` | | **9** | `Node.DOCUMENT_NODE` | | **10** | `Node.DOCUMENT_TYPE_NODE` | | **11** | `Node.DOCUMENT_FRAGMENT_NODE` | | **12** | `Node.NOTATION_NODE` | --- ## Code Examples ### Example 1: Checking Node Types and Names This JavaScript example loads an XML document and iterates through the child nodes of the root element, outputting their `nodeName` and `nodeType`. ```javascript // Load the XML document const xmlDoc = loadXMLDoc("books.xml"); // Get the child nodes of the root element () const childNodes = xmlDoc.documentElement.childNodes; for (let i = 0; i < childNodes.length; i++) { const node = childNodes; console.log(`Node Name: ${node.nodeName} | Node Type: ${node.nodeType}`); } ``` ### Example 2: Retrieving Node Values This example demonstrates how to retrieve the text content of elements. Note that the text inside an element is actually stored in a child **Text Node** (`nodeType === 3`). ```javascript const xmlDoc = loadXMLDoc("books.xml"); // Select the first element const titleElement = xmlDoc.getElementsByTagName("title"); // The element itself has a nodeValue of null console.log("Element Node Value:", titleElement.nodeValue); // Output: null // To get the text, we access its child text node const textNode = titleElement.firstChild; console.log("Text Node Name:", textNode.nodeName); // Output: #text console.log("Text Node Value:", textNode.nodeValue); // Output: Everyday Italian ``` ### Example 3: Filtering Elements Using Named Constants You can use the named constants to safely filter out whitespace text nodes and comments when traversing the DOM: ```javascript const container = document.getElementById("my-container"); container.childNodes.forEach(node => { if (node.nodeType === Node.ELEMENT_NODE) { console.log("Found Element:", node.tagName); } else if (node.nodeType === Node.TEXT_NODE) { console.log("Found Text Node:", JSON.stringify(node.nodeValue)); } }); ``` --- ## Important Considerations 1. **Whitespace as Text Nodes**: In both HTML and XML, whitespace (spaces, tabs, and newlines) between tags is parsed as `TEXT_NODE` (type 3) elements. When traversing the DOM using properties like `firstChild` or `nextSibling`, you must account for these empty text nodes. 2. **Modern Alternatives**: To avoid dealing with whitespace text nodes, modern web APIs provide element-only traversal properties such as `firstElementChild`, `lastElementChild`, `nextElementSibling`, and `children`. 3. **Deprecated Nodes**: Some node types (like `Node.ENTITY_NODE` and `Node.NOTATION_NODE`) are legacy XML features and are deprecated or not used in modern HTML5 environments.</div><div class="post-nav"><a href="/post/dom-node.html">← Dom Node</a><a href="/post/dom-nodes-create.html">Dom Nodes Create β†’</a></div></div> <div class="sidebar"><div class="sidebar-widget"><h3>πŸ“‚ Categories</h3><ul><li><a href="/category/18.html">⚑ JavaScript</a> <span style="color:#999;font-size:12px">(1589)</span></li><li><a href="/category/23.html">🐘 PHP</a> <span style="color:#999;font-size:12px">(872)</span></li><li><a href="/category/27.html">🐍 Python3</a> <span style="color:#999;font-size:12px">(810)</span></li><li><a href="/category/15.html">🌐 HTML</a> <span style="color:#999;font-size:12px">(691)</span></li><li><a href="/category/31.html">βš™οΈ C#</a> <span style="color:#999;font-size:12px">(650)</span></li><li><a href="/category/26.html">🐍 Python</a> <span style="color:#999;font-size:12px">(594)</span></li><li><a href="/category/28.html">β˜• Java</a> <span style="color:#999;font-size:12px">(552)</span></li><li><a href="/category/72.html">βš™οΈ PyTorch</a> <span style="color:#999;font-size:12px">(534)</span></li><li><a href="/category/75.html">🐧 Linux</a> <span style="color:#999;font-size:12px">(472)</span></li><li><a href="/category/29.html">βš™οΈ C</a> <span style="color:#999;font-size:12px">(432)</span></li><li><a href="/category/20.html">πŸ“¦ jQuery</a> <span style="color:#999;font-size:12px">(406)</span></li><li><a href="/category/16.html">🎨 CSS</a> <span style="color:#999;font-size:12px">(377)</span></li><li><a href="/category/64.html">πŸ“„ XML</a> <span style="color:#999;font-size:12px">(259)</span></li><li><a href="/category/59.html">πŸ“¦ jQuery UI</a> <span style="color:#999;font-size:12px">(231)</span></li><li><a href="/category/60.html">🎯 Bootstrap</a> <span style="color:#999;font-size:12px">(220)</span></li><li><a href="/category/30.html">βš™οΈ C++</a> <span style="color:#999;font-size:12px">(215)</span></li><li><a href="/category/58.html">πŸ…°οΈ Angular</a> <span style="color:#999;font-size:12px">(205)</span></li><li><a href="/category/19.html">🌐 HTML DOM</a> <span style="color:#999;font-size:12px">(201)</span></li><li><a href="/category/50.html">πŸ”΄ Redis</a> <span style="color:#999;font-size:12px">(188)</span></li><li><a href="/category/90.html">πŸ“– Web Building</a> <span style="color:#999;font-size:12px">(142)</span></li><li><a href="/category/57.html">πŸ’š Vue.js</a> <span style="color:#999;font-size:12px">(141)</span></li><li><a href="/category/43.html">πŸ“ˆ R</a> <span style="color:#999;font-size:12px">(131)</span></li><li><a href="/category/69.html">🐼 Pandas</a> <span style="color:#999;font-size:12px">(124)</span></li><li><a href="/category/24.html">πŸ—„οΈ SQL</a> <span style="color:#999;font-size:12px">(105)</span></li><li><a href="/category/76.html">βš™οΈ Docker</a> <span style="color:#999;font-size:12px">(86)</span></li><li><a href="/category/36.html">βš™οΈ TypeScript</a> <span style="color:#999;font-size:12px">(73)</span></li><li><a href="/category/85.html">βš™οΈ Highcharts</a> <span style="color:#999;font-size:12px">(70)</span></li><li><a href="/category/95.html">πŸ“– AI Agent</a> <span style="color:#999;font-size:12px">(70)</span></li><li><a href="/category/56.html">βš™οΈ React</a> <span style="color:#999;font-size:12px">(68)</span></li><li><a href="/category/35.html">πŸ“– Node.js</a> <span style="color:#999;font-size:12px">(65)</span></li><li><a href="/category/94.html">βš™οΈ Machine Learning</a> <span style="color:#999;font-size:12px">(60)</span></li><li><a href="/category/77.html">πŸ“ Git</a> <span style="color:#999;font-size:12px">(59)</span></li><li><a href="/category/32.html">πŸ”΅ Go</a> <span style="color:#999;font-size:12px">(58)</span></li><li><a href="/category/83.html">πŸ“ˆ Markdown</a> <span style="color:#999;font-size:12px">(58)</span></li><li><a href="/category/68.html">πŸ”’ NumPy</a> <span style="color:#999;font-size:12px">(55)</span></li><li><a href="/category/66.html">πŸ§ͺ Flask</a> <span style="color:#999;font-size:12px">(54)</span></li><li><a href="/category/41.html">βš™οΈ Scala</a> <span style="color:#999;font-size:12px">(53)</span></li><li><a href="/category/48.html">πŸ—„οΈ SQLite</a> <span style="color:#999;font-size:12px">(52)</span></li><li><a href="/category/116.html">πŸ“– JSTL</a> <span style="color:#999;font-size:12px">(52)</span></li><li><a href="/category/106.html">βš™οΈ VS Code</a> <span style="color:#999;font-size:12px">(51)</span></li><li><a href="/category/49.html">πŸƒ MongoDB</a> <span style="color:#999;font-size:12px">(49)</span></li><li><a href="/category/42.html">πŸ“ˆ Perl</a> <span style="color:#999;font-size:12px">(48)</span></li><li><a href="/category/34.html">πŸ“ˆ Ruby</a> <span style="color:#999;font-size:12px">(47)</span></li><li><a href="/category/70.html">πŸ“Š Matplotlib</a> <span style="color:#999;font-size:12px">(47)</span></li><li><a href="/category/1.html">βš™οΈ Uncategorized</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/37.html">🍎 Swift</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/51.html">πŸ—„οΈ PostgreSQL</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/88.html">βš™οΈ Data Structures</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/100.html">πŸ“ˆ Playwright</a> <span style="color:#999;font-size:12px">(46)</span></li><li><a href="/category/54.html">πŸ“– iOS</a> <span style="color:#999;font-size:12px">(45)</span></li><li><a href="/category/25.html">πŸ—„οΈ MySQL</a> <span style="color:#999;font-size:12px">(44)</span></li><li><a href="/category/108.html">βš™οΈ LangChain</a> <span style="color:#999;font-size:12px">(43)</span></li><li><a href="/category/67.html">πŸ“– FastAPI</a> <span style="color:#999;font-size:12px">(40)</span></li><li><a href="/category/112.html">βš™οΈ Ionic</a> <span style="color:#999;font-size:12px">(38)</span></li><li><a href="/category/87.html">πŸ“ˆ Design Patterns</a> <span style="color:#999;font-size:12px">(37)</span></li><li><a href="/category/114.html">βš™οΈ Eclipse</a> <span style="color:#999;font-size:12px">(37)</span></li><li><a href="/category/17.html">🎨 CSS3</a> <span style="color:#999;font-size:12px">(34)</span></li><li><a href="/category/40.html">πŸŒ™ Lua</a> <span style="color:#999;font-size:12px">(34)</span></li><li><a href="/category/127.html">βš™οΈ Codex</a> <span style="color:#999;font-size:12px">(34)</span></li><li><a href="/category/65.html">🎸 Django</a> <span style="color:#999;font-size:12px">(32)</span></li><li><a href="/category/102.html">βš™οΈ OpenCV</a> <span style="color:#999;font-size:12px">(32)</span></li><li><a href="/category/33.html">πŸ“ˆ Rust</a> <span style="color:#999;font-size:12px">(31)</span></li><li><a href="/category/117.html">πŸ“– JSP</a> <span style="color:#999;font-size:12px">(31)</span></li><li><a href="/category/126.html">βš™οΈ Claude Code</a> <span style="color:#999;font-size:12px">(31)</span></li><li><a href="/category/101.html">πŸ“– Pillow</a> <span style="color:#999;font-size:12px">(30)</span></li><li><a href="/category/130.html">βš™οΈ OpenCode</a> <span style="color:#999;font-size:12px">(28)</span></li><li><a href="/category/131.html">πŸ“– AI Skills</a> <span style="color:#999;font-size:12px">(27)</span></li><li><a href="/category/55.html">πŸ“ˆ Flutter</a> <span style="color:#999;font-size:12px">(26)</span></li><li><a href="/category/80.html">πŸ“– Maven</a> <span style="color:#999;font-size:12px">(26)</span></li><li><a href="/category/61.html">🎨 Tailwind CSS</a> <span style="color:#999;font-size:12px">(25)</span></li><li><a href="/category/73.html">🧠 TensorFlow</a> <span style="color:#999;font-size:12px">(25)</span></li><li><a href="/category/118.html">πŸ“ˆ Servlet</a> <span style="color:#999;font-size:12px">(24)</span></li><li><a href="/category/39.html">πŸ“ˆ Dart</a> <span style="color:#999;font-size:12px">(23)</span></li><li><a href="/category/46.html">πŸ“– Assembly</a> <span style="color:#999;font-size:12px">(23)</span></li><li><a href="/category/52.html">βš™οΈ Memcached</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/63.html">✏️ SVG</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/98.html">βš™οΈ Electron</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/103.html">πŸ“– NLP</a> <span style="color:#999;font-size:12px">(22)</span></li><li><a href="/category/82.html">πŸ” Regex</a> <span style="color:#999;font-size:12px">(21)</span></li><li><a href="/category/53.html">πŸ“ˆ Android</a> <span style="color:#999;font-size:12px">(20)</span></li><li><a href="/category/38.html">🟣 Kotlin</a> <span style="color:#999;font-size:12px">(19)</span></li><li><a href="/category/44.html">πŸ“– Julia</a> <span style="color:#999;font-size:12px">(19)</span></li><li><a href="/category/92.html">πŸ“– SOAP</a> <span style="color:#999;font-size:12px">(17)</span></li><li><a href="/category/99.html">πŸ“– Selenium</a> <span style="color:#999;font-size:12px">(17)</span></li><li><a href="/category/104.html">πŸ“ˆ PowerShell</a> <span style="color:#999;font-size:12px">(17)</span></li><li><a href="/category/62.html">πŸ’… Sass</a> <span style="color:#999;font-size:12px">(16)</span></li><li><a href="/category/89.html">πŸ“– HTTP</a> <span style="color:#999;font-size:12px">(16)</span></li><li><a href="/category/45.html">πŸ“– Zig</a> <span style="color:#999;font-size:12px">(15)</span></li><li><a href="/category/113.html">πŸ“– AI</a> <span style="color:#999;font-size:12px">(15)</span></li><li><a href="/category/21.html">πŸ”„ AJAX</a> <span style="color:#999;font-size:12px">(14)</span></li><li><a href="/category/110.html">πŸ“ˆ Swagger</a> <span style="color:#999;font-size:12px">(14)</span></li><li><a href="/category/74.html">βš™οΈ Scikit-learn</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/84.html">βš™οΈ ECharts</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/86.html">βš™οΈ Chart.js</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/128.html">βš™οΈ Cursor</a> <span style="color:#999;font-size:12px">(13)</span></li><li><a href="/category/71.html">βš™οΈ SciPy</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/91.html">πŸ“ˆ RDF</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/96.html">πŸ“– Ollama</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/97.html">πŸ“– Next.js</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/129.html">πŸ“– Plotly Dash</a> <span style="color:#999;font-size:12px">(12)</span></li><li><a href="/category/22.html">πŸ“‹ JSON</a> <span style="color:#999;font-size:12px">(11)</span></li><li><a href="/category/109.html">πŸ“ˆ RESTful API</a> <span style="color:#999;font-size:12px">(11)</span></li><li><a href="/category/93.html">πŸ“– WSDL</a> <span style="color:#999;font-size:12px">(9)</span></li><li><a href="/category/81.html">βš™οΈ CMake</a> <span style="color:#999;font-size:12px">(8)</span></li><li><a href="/category/115.html">πŸ“ˆ Firebug</a> <span style="color:#999;font-size:12px">(7)</span></li><li><a href="/category/78.html">πŸ“– Nginx</a> <span style="color:#999;font-size:12px">(6)</span></li><li><a href="/category/79.html">☸️ Kubernetes</a> <span style="color:#999;font-size:12px">(6)</span></li><li><a href="/category/107.html">πŸ“ˆ Jupyter</a> <span style="color:#999;font-size:12px">(6)</span></li><li><a href="/category/105.html">πŸ“– LaTeX</a> <span style="color:#999;font-size:12px">(4)</span></li><li><a href="/category/111.html">πŸ“– UniApp</a> <span style="color:#999;font-size:12px">(4)</span></li><li><a href="/category/47.html">πŸ—„οΈ SQL Server</a> <span style="color:#999;font-size:12px">(1)</span></li></ul></div></div> </div> </div> <footer> <div class="container"> <p>YouTip © 2024-2026 | <a href="/">Home</a> | Learn Technology, Build Dreams!</p> <p>All content is for educational and learning purposes only.</p> </div> </footer> <!-- Global Analytics Tracker --> <script> // Placeholder for Google Analytics </script> </body> </html>