"); } ``` ### Output ```text category = COOKING (nodetype: 2) category = CHILDREN (nodetype: 2) category = WEB (nodetype: 2) category = WEB (nodetype: 2) ``` --- ## Technical Considerations & Reference ### Node Type Constants Reference While the `nodeType` of an `Attr` node is always `2`, it is helpful to understand how this fits into the broader XML DOM node type classification: | Node Type Constant | Integer Value | Description | | :--- | :--- | :--- | | `Node.ELEMENT_NODE` | **1** | An Element node (e.g., `
Prop Attr Nodetype
## XML DOM: The `nodeType` Property of the Attr Object
In the XML Document Object Model (DOM), the `nodeType` property is a fundamental property used to identify the specific type of a DOM node. When accessed on an `Attr` (Attribute) object, it returns a predefined integer representing the attribute node type.
---
## Definition and Usage
The `nodeType` property returns an integer value that represents the node type of the specified attribute.
For all attribute nodes, this property always returns the integer value **`2`**, which corresponds to the constant `Node.ATTRIBUTE_NODE`.
### Syntax
```javascript
attrObject.nodeType
```
### Return Value
* **Type:** `Number`
* **Value:** `2` (representing `Node.ATTRIBUTE_NODE`)
---
## Code Example
The following example demonstrates how to load an XML document (`books.xml`) and retrieve the name, value, and node type of the first attribute for each `` element.
### Sample XML File (`books.xml`)
```xml
Everyday Italian
Giada De Laurentiis
2005
30.00
Harry Potter
J K. Rowling
2005
29.99
```
### JavaScript Implementation
```javascript
// Load the XML document
xmlDoc = loadXMLDoc("books.xml");
// Get all elements
var x = xmlDoc.getElementsByTagName('book');
for (var i = 0; i < x.length; i++) {
// Access the first attribute of the current element
var attribute = x.item(i).attributes;
// Output the attribute name, value, and its nodeType
document.write(attribute.nodeName);
document.write(" = ");
document.write(attribute.nodeValue);
document.write(" (nodetype: " + attribute.nodeType + ")");
document.write("
"); } ``` ### Output ```text category = COOKING (nodetype: 2) category = CHILDREN (nodetype: 2) category = WEB (nodetype: 2) category = WEB (nodetype: 2) ``` --- ## Technical Considerations & Reference ### Node Type Constants Reference While the `nodeType` of an `Attr` node is always `2`, it is helpful to understand how this fits into the broader XML DOM node type classification: | Node Type Constant | Integer Value | Description | | :--- | :--- | :--- | | `Node.ELEMENT_NODE` | **1** | An Element node (e.g., ``) |
| `Node.ATTRIBUTE_NODE` | **2** | An Attribute node (e.g., `category="COOKING"`) |
| `Node.TEXT_NODE` | **3** | The actual text content inside an element or attribute |
| `Node.CDATA_SECTION_NODE` | **4** | A CDATA Section node |
| `Node.COMMENT_NODE` | **8** | A Comment node |
| `Node.DOCUMENT_NODE` | **9** | The root Document node |
### Best Practices
* **Read-Only:** The `nodeType` property is read-only. Attempting to modify it will result in an error or be silently ignored.
* **Modern Web Standards Note:** In modern HTML5 DOM specifications, the `Attr` interface no longer inherits from `Node` in some environments, and direct attribute manipulation is often handled via `getAttribute()` and `setAttribute()`. However, in XML DOM parsing and legacy systems, checking `nodeType === 2` remains a standard way to filter out attribute nodes during manual DOM tree traversals.
"); } ``` ### Output ```text category = COOKING (nodetype: 2) category = CHILDREN (nodetype: 2) category = WEB (nodetype: 2) category = WEB (nodetype: 2) ``` --- ## Technical Considerations & Reference ### Node Type Constants Reference While the `nodeType` of an `Attr` node is always `2`, it is helpful to understand how this fits into the broader XML DOM node type classification: | Node Type Constant | Integer Value | Description | | :--- | :--- | :--- | | `Node.ELEMENT_NODE` | **1** | An Element node (e.g., `
YouTip