dd

XML DOM nodeType 属性


定义和用法

nodeType 属性返回选定节点的节点类型。

语法

elementNode.nodeType


节点数量:节点名称:
1Element
2Attribute
3Text
4CDATA Section
5Entity Reference
6Entity
7Processing Instruction
8Comment
9Document
10Document Type
11Document Fragment
12Notation

实例

下面的代码片段使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中,并从第一个 <title> 元素取得节点类型:

实例

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0];
document.write(x.nodeType);

上面的代码将输出:

1
 

尝试一下 Demos

nodeType - 忽略空的文本节点

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>
<script>
//check if the first node is an element node
function get_firstchild(n)
{
x=n.firstChild;
while (x.nodeType!=1)
  {
  x=x.nextSibling;
  }
return x;
}

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement;
firstNode=get_firstchild(x);

for (i=0;i<firstNode.childNodes.length;i++)
{ 
if (firstNode.childNodes[i].nodeType==1)
  { 
  //Process only element nodes
  document.write(firstNode.childNodes[i].nodeName);
  document.write(" = ");
  document.write(firstNode.childNodes[i].childNodes[0].nodeValue);
  document.write("<br>");
  } 
}

</script>
</body>
</html>