Dom 简明教程

XML DOM - Model

既然我们了解了 DOM 的含义,让我们看看 DOM 结构是什么。DOM 文档是按层次结构组织的一系列节点或信息片段。某些类型的节点可能有各种类型的子节点,而其他类型是叶子节点,在文档结构中没有子节点。以下是节点类型的列表,以及它们作为子节点可以具有的节点类型的列表 -

Now that we know what DOM means, let’s see what a DOM structure is. A DOM document is a collection of nodes or pieces of information, organized in a hierarchy. Some types of nodes may have child nodes of various types and others are leaf nodes that cannot have anything under them in the document structure. Following is a list of the node types, with a list of node types that they may have as children −

  1. Document − Element (maximum of one), ProcessingInstruction, Comment, DocumentType (maximum of one)

  2. DocumentFragment − Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference

  3. EntityReference − Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference

  4. Element − Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference

  5. Attr − Text, EntityReference

  6. ProcessingInstruction − No children

  7. Comment − No children

  8. Text − No children

  9. CDATASection − No children

  10. Entity − Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference

  11. Notation − No children

Example

考虑下列 XML 文档 node.xml 的 DOM 表示。

Consider the DOM representation of the following XML document node.xml.

<?xml version = "1.0"?>
<Company>
   <Employee category = "technical">
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
   </Employee>

   <Employee category = "non-technical">
      <FirstName>Taniya</FirstName>
      <LastName>Mishra</LastName>
      <ContactNo>1234667898</ContactNo>
   </Employee>
</Company>

上述 XML 文档的文档对象模型如下 -

The Document Object Model of the above XML document would be as follows −

xml dom model

从上图流程中,我们可以推断 -

From the above flowchart, we can infer −

  1. Node object can have only one parent node object. This occupies the position above all the nodes. Here it is Company.

  2. The parent node can have multiple nodes called the child nodes. These child nodes can have additional nodes called the attribute nodes. In the above example, we have two attribute nodes Technical and Non-technical. The attribute node is not actually a child of the element node, but is still associated with it.

  3. These child nodes in turn can have multiple child nodes. The text within the nodes is called the text node.

  4. The node objects at the same level are called as siblings.

  5. The DOM identifies − the objects to represent the interface and manipulate the document. the relationship among the objects and interfaces.