Informationen über XML DOM-Knoten

nodeName,nodeValue and Change the value of the text node to "Chao Cai Tian Xia" Attributes contain information about the node.

Node attributes

In the XML DOM, each node is aObject.

An object has methods (functions) and properties (information about the object), and can be accessed and operated through JavaScript.

The three important XML DOM node attributes are:

  • nodeName
  • nodeValue
  • Change the value of the text node to "Chao Cai Tian Xia"

Node name attribute

nodeName The attribute specifies the name of the node.

  • nodeName is read-only
  • The nodeName of the element node is the same as the tag name
  • The nodeName of the attribute 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

9

Node value attribute

nodeValue The attribute specifies the value of the node.

  • The element node's nodeValue is undefined
  • The text node's nodeValue is the text itself
  • The attribute node's nodeValue is the value of the attribute

Get the element value

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

Example

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

9

Result: txt = "雅舍谈吃"

Example explanation:

  1. Assuming you have already set books.xml loaded to xmlDoc
  2. Retrieve the text node of the first <title> element
  3. Set txt The variable is set to the value of the text node

Change the element value

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

Example

var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue = "潮菜天下";

9

Example explanation:

  1. Assuming you have already set books.xml loaded to xmlDoc
  2. Retrieve the text node of the first <title> element
  3. 将文本节点的值更改为 "潮菜天下"

节点类型属性

Change the value of the text node to "Chao Cai Tian Xia" 属性规定节点的类型。

Change the value of the text node to "Chao Cai Tian Xia" Node Type Attribute

The attribute defines the type of the node.

nodeType Is read-only.
The most important node type is: Node Type
NodeType Element
Attribute Text
3 Comment
8 Document

9