阅读 (0)

XML DOM 节点类型(Node Types)

实例

在下面的例子中,我们将使用 XML 文件 books.xml,以及 JavaScript 函数 loadXMLDoc()。

显示所有元素的节点名称和节点类型
显示所有元素的节点名称和节点值

节点类型

下面的表格列出了不同的 W3C 节点类型,以及它们可拥有的子元素:

节点类型描述子元素
Document表示整个文档(DOM 树的根节点)
  • Element (max. one)

  • ProcessingInstruction

  • Comment

  • DocumentType

DocumentFragment表示轻量级的 Document 对象,其中容纳了一部分文档。
  • ProcessingInstruction

  • Comment

  • Text

  • CDATASection

  • EntityReference

DocumentType向为文档定义的实体提供接口。None
ProcessingInstruction表示处理指令。None
EntityReference表示实体引用元素。
  • ProcessingInstruction

  • Comment

  • Text

  • CDATASection

  • EntityReference

Element表示 element(元素)元素
  • Text

  • Comment

  • ProcessingInstruction

  • CDATASection

  • EntityReference

Attr表示属性。
  • Text

  • EntityReference

Text表示元素或属性中的文本内容。None
CDATASection表示文档中的 CDATA 区段(文本不会被解析器解析)None
Comment表示注释。None
Entity表示实体。
  • ProcessingInstruction

  • Comment

  • Text

  • CDATASection

  • EntityReference

Notation表示在 DTD 中声明的符号。None

节点类型 - 所返回的值

下面的表格列出了对每个节点类型来说,nodeName 和 nodeValue 属性可返回的值:

节点类型nodeName 的返回值nodeValue 的返回值
Document#documentnull
DocumentFragment#document fragmentnull
DocumentTypedoctype 名称null
EntityReference实体引用名称null
Elementelement namenull
Attr属性名称属性值
ProcessingInstructiontarget节点的内容
Comment#comment注释文本
Text#text节点内容
CDATASection#cdata-section节点内容
Entity实体名称null
Notation符号名称null

NodeTypes - 有名常数

NodeTypeNamed Constant
1ELEMENT_NODE
2ATTRIBUTE_NODE
3TEXT_NODE
4CDATA_SECTION_NODE
5ENTITY_REFERENCE_NODE
6ENTITY_NODE
7PROCESSING_INSTRUCTION_NODE
8COMMENT_NODE
9DOCUMENT_NODE
10DOCUMENT_TYPE_NODE
11DOCUMENT_FRAGMENT_NODE
12NOTATION_NODE