XML DOM Node Information
The nodeName, nodeValue, and nodeType properties contain information about a node.
Try it - Example
The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, used to load the XML file.
Get an Element Node's Node Name
This example uses the nodeName property to get the node name of the root element in "books.xml".
Get Text from a Text Node
This example uses the nodeValue property to get the first element in "books.xml"
Change the text in a text node
This example uses the nodeValue property to change the first element in "books.xml"
Get the node name and type of an element node
This example uses the nodeName and nodeType properties to get the node name and type of the root element in "books.xml".
The node's attributes
In the XML DOM, every node is aObject.
An object has methods and properties, and can be accessed and manipulated through JavaScript.
The three important node properties are:
- nodeName
- nodeValue
- nodeType
nodeName Attribute
The nodeName property specifies the name of a node.
- nodeName is read-only
- the nodeName of an element node is the same as the tag name
- the nodeName of an attribute node is the name of the attribute
- the nodeName of a text node is always #text
- the nodeName of a document node is always #document
nodeValue Attribute
The nodeValue property specifies the value of a node.
- the nodeValue of an element node is undefined
- the nodeValue of a text node is the text itself
- the nodeValue of an attribute node is the value of the attribute
Get the value of an element
The following code retrieves the first
Example
xmlDoc
=
loadXMLDoc
(
"
books.xml
"
)
;
x
=
xmlDoc
.
getElementsByTagName
(
"
title
"
)
[
0
]
.
childNodes
[
0
]
;
txt
=
x
.
nodeValue
;
Try it »
Result: txt = "Everyday Italian"
Example explained:
- Use loadXMLDoc() Put the "
Change an Element's Value
The following code changes the first
Example
xmlDoc
=
loadXMLDoc
(
"
books.xml
"
)
;
x
=
xmlDoc
.
getElementsByTagName
(
"
title
"
)
[
0
]
.
childNodes
[
0
]
;
x
.
nodeValue
=
"
Easy Cooking
"
;
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Get the first
element node's text node
The nodeType Property
The nodeType property specifies the type of the node.
nodeType is read-only.
The most important node types are:
| Node types | NodeType |
|---|---|
| Element | 1 |
| property | 2 |
| Text | 3 |
| Notes | 8 |
| Document | 9 |