Nodetype: " + xmlDoc.nodeType); ``` ### Output ```text Nodename: #document Nodetype: 9 ``` --- ## Practical Considerations 1. **Cross-Browser Consistency:** The integer values returned by `nodeType` are standardized by the W3C and are consistent across all modern web browsers and XML parsers. 2. **Filtering Nodes:** When traversing child nodes using `childNodes`, the list includes text nodes (which can be empty whitespace) and comment nodes. You can use `nodeType` to filter out everything except element nodes: ```javascript var children = xmlDoc.documentElement.childNodes; for (var i = 0; i < children.length; i++) { if (children.nodeType === 1) { // 1 = ELEMENT_NODE console.log("Found element node: " + children.nodeName); } } ``` 3. **Read-Only:** The `nodeType` property is read-only. Attempting to assign a value to it will fail or be ignored.
Prop Document Nodetype
## XML DOM nodeType Property
The `nodeType` property returns an integer representing the specific type of the selected node. This property is read-only and is highly useful for distinguishing between different types of nodes (such as elements, attributes, text, and documents) when traversing the DOM tree.
---
## Definition and Usage
In the XML DOM, everything is a node. The `nodeType` property allows you to programmatically determine what kind of node you are currently dealing with.
For the **Document** object itself, the `nodeType` always returns **`9`** (which represents `Node.DOCUMENT_NODE`).
### Common Node Type Constants
| Node Type Constant | Integer Value | Description |
| :--- | :--- | :--- |
| `ELEMENT_NODE` | **1** | An Element node (e.g., ``, ``) |
| `ATTRIBUTE_NODE` | **2** | An Attribute node (e.g., `category="cooking"`) |
| `TEXT_NODE` | **3** | The actual text content inside an element or attribute |
| `CDATA_SECTION_NODE` | **4** | A CDATA section node |
| `ENTITY_REFERENCE_NODE` | **5** | An Entity Reference node |
| `ENTITY_NODE` | **6** | An Entity node |
| `PROCESSING_INSTRUCTION_NODE` | **7** | A Processing Instruction node |
| `COMMENT_NODE` | **8** | A Comment node (e.g., ``) |
| `DOCUMENT_NODE` | **9** | The Document node (the root of the DOM tree) |
| `DOCUMENT_TYPE_NODE` | **10** | The Document Type node (e.g., ``) |
| `DOCUMENT_FRAGMENT_NODE` | **11** | A Document Fragment node |
| `NOTATION_NODE` | **12** | A Notation node |
---
## Syntax
```javascript
documentObject.nodeType
```
### Return Value
* **Type:** `Number`
* **Description:** An integer representing the node type (e.g., `9` for the Document node).
---
## Code Example
In this example, we will load the XML file [books.xml](/try/demo_source/books.xml) using the helper function `loadXMLDoc()` and retrieve both the node name and node type of the root document.
### Example Code
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Output the node name and node type of the Document object
document.write("Nodename: " + xmlDoc.nodeName);
document.write("
Nodetype: " + xmlDoc.nodeType); ``` ### Output ```text Nodename: #document Nodetype: 9 ``` --- ## Practical Considerations 1. **Cross-Browser Consistency:** The integer values returned by `nodeType` are standardized by the W3C and are consistent across all modern web browsers and XML parsers. 2. **Filtering Nodes:** When traversing child nodes using `childNodes`, the list includes text nodes (which can be empty whitespace) and comment nodes. You can use `nodeType` to filter out everything except element nodes: ```javascript var children = xmlDoc.documentElement.childNodes; for (var i = 0; i < children.length; i++) { if (children.nodeType === 1) { // 1 = ELEMENT_NODE console.log("Found element node: " + children.nodeName); } } ``` 3. **Read-Only:** The `nodeType` property is read-only. Attempting to assign a value to it will fail or be ignored.
Nodetype: " + xmlDoc.nodeType); ``` ### Output ```text Nodename: #document Nodetype: 9 ``` --- ## Practical Considerations 1. **Cross-Browser Consistency:** The integer values returned by `nodeType` are standardized by the W3C and are consistent across all modern web browsers and XML parsers. 2. **Filtering Nodes:** When traversing child nodes using `childNodes`, the list includes text nodes (which can be empty whitespace) and comment nodes. You can use `nodeType` to filter out everything except element nodes: ```javascript var children = xmlDoc.documentElement.childNodes; for (var i = 0; i < children.length; i++) { if (children.nodeType === 1) { // 1 = ELEMENT_NODE console.log("Found element node: " + children.nodeName); } } ``` 3. **Read-Only:** The `nodeType` property is read-only. Attempting to assign a value to it will fail or be ignored.
YouTip