Latest web development tutorials

XML DOM node information

nodeName, nodeValue, and nodeType attribute contains information about the node.


Examples

Try - Example

The following example uses XML files the Books.xml .
Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

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

Get text from text node
This example uses the nodeValue property to get the text "books.xml" the first <title> element.

Change text node text
This example uses the nodeValue property to change the text "books.xml" the first <title> element.

Gets the element node with name and type
This example uses the nodeName and nodeType property to get "books.xml" node name and type of the root element.


Attribute node

In the XML DOM, each node is anobject.

Objects have methods and properties can be accessed and manipulated using JavaScript.

Three important node attributes are:

  • nodeName
  • nodeValue
  • nodeType

nodeName property

NodeName attribute specifies the name of the node.

  • nodeName is a read-only
  • nodeName with the same tag name of an element node
  • nodeName attribute node is the name of the property
  • nodeName text node is always #text
  • nodeName document node is always #document

try it.


nodeValue property

NodeValue property value specified node.

  • nodeValue element node is undefined
  • nodeValue text node is the text itself
  • nodeValue attribute node is the value of the property

Gets the value of the element

The following code retrieves a text node of the first <title> element values:

Examples

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;

try it"

Results: txt = "Everyday Italian"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Gets the first text node <title> element node
  3. The txt variable to the value of the text node

Change the value of the element

The following code changes the first <title> element text node values:

Examples

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Gets the first text node <title> element node
  3. Change the text node values ​​"Easy Cooking"

nodeType property

NodeType attribute specifies the type of node.

nodeType is read-only.

The most important node types are:

节点类型 NodeType
元素 1
属性 2
文本 3
注释 8
文档 9

try it.