DOM node information
XML DOM Node information
The nodeName, nodeValue, and nodeType attributes contain information about the node.
Try itTry it - Example
Function loadXMLDoc(), located in external JavaScript, is used to load XML files.
This example uses the nodeName attribute to get the node name of the root element in "books.xml".
This example uses the nodeValue attribute to get the text of the first <title> element in "books.xml".
This example uses the nodeValue attribute to change the text of the first <title> element in "books.xml".
This example uses the nodeName and nodeType attributes to get the node name and type of the root element in "books.xml".
Attributes of nodesIn XML DOM, each node is an
object.
Objects have methods and properties that can be accessed and manipulated through JavaScript. The three important node attributes are:- nodeName ##nodeValue
- nodeType
The nodeName attribute specifies the name of the node.
- nodeName is read-only
- The nodeName of the element node is the same as the label name
- Attribute The nodeName of the node is the name of the attribute
- The nodeName of the text node is always #text
- The nodeName of the document node is always #document
The nodeValue attribute specifies the value of the node.
- The nodeValue of the element node is undefined
- The nodeValue of the text node is the text itself
- Attribute The nodeValue of the node is the value of the attribute
<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
document.write(xmlDoc.documentElement.nodeName);
</script>
</body>
</html>
Running instance »Click the "Run Example" button to view the online example
The following code retrieves the first <title> element The value of the text node:
Instance<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write(txt);
</script>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Result: txt = "Everyday Italian"
Explanation of examples:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Get the text node of the first <title> element node
Set the txt variable to the value of the text node
Change the value of the element
The following code changes the value of the text node of the first <title> element:
Example
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
Instance explanation:
-
Use loadXMLDoc() to load "books.xml" into xmlDoc
Get the text node of the first <title> element node
Change the value of the text node to "Easy Cooking"
nodeType attribute
The nodeType attribute specifies the type of node.
nodeType is read-only.
The most important node types are:
Node Type | NodeType |
---|---|
Element | 1 |
Attributes | 2 |
3 | |
8 | |
9 |
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write(xmlDoc.documentElement.nodeName); document.write("<br>"); document.write(xmlDoc.documentElement.nodeType); </script> </body> </html>
Run Instance»Click the "Run Instance" button to view the online instance