`). | `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.
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 `
YouTip