XML DOM tutoria...login
XML DOM tutorial
author:php.cn  update time:2022-04-13 15:27:56

DOM node information


XML DOM Node information


The nodeName, nodeValue, and nodeType attributes contain information about the node.


tryitimg.gifTry itTry it - Example


##The following example uses the XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, is used to load XML files.

Get the node name of the element node

This example uses the nodeName attribute to get the node name of the root element in "books.xml".

Get text from text node

This example uses the nodeValue attribute to get the text of the first <title> element in "books.xml".

Change the text in the text node

This example uses the nodeValue attribute to change the text of the first <title> element in "books.xml".

Get the node name and type of the element node

This example uses the nodeName and nodeType attributes to get the node name and type of the root element in "books.xml".


Attributes of nodes

In 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
nodeName attribute

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
nodeValue attribute

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

Instance

<!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

Get the value of the element

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:

  1. Use loadXMLDoc() to load "books.xml" into xmlDoc

  2. Get the text node of the first <title> element node

  3. 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:

  1. Use loadXMLDoc() to load "books.xml" into xmlDoc

  2. Get the text node of the first <title> element node

  3. 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:

##Text3Comments8Documentation9
Node TypeNodeType
Element 1
Attributes2
Instance

<!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