Latest web development tutorials

XML DOM - Document Object

Document object represents the entire XML document.


Document Object

Document object is the root of the document tree, and provides the initial (or top-level) access to document data entry for us.

Since element nodes, text nodes, comments, processing instructions, etc. can not exist outside of the document, Document object also provides methods to create these objects. Node object provides a ownerDocument property, which can create them and associate them Document therein.

Document Object Properties

属性 描述
async 规定 XML 文件的下载是否应当被异步处理。
childNodes 返回文档的子节点的节点列表。
doctype 返回与文档相关的文档类型声明(DTD,全称 Document Type Declaration)。
documentElement 返回文档的根节点。
documentURI 设置或返回文档的位置。
domConfig 返回 normalizeDocument() 被调用时所使用的配置。
firstChild 返回文档的第一个子节点。
implementation 返回处理该文档的 DOMImplementation 对象。
inputEncoding 返回用于文档的编码方式(在解析时)。
lastChild 返回文档的最后一个子节点。
nodeName 返回节点的名称(根据节点的类型)。
nodeType 返回节点的节点类型。
nodeValue 设置或返回节点的值(根据节点的类型)。
strictErrorChecking 设置或返回是否强制进行错误检查。
xmlEncoding 返回文档的 XML 编码。
xmlStandalone 设置或返回文档是否为 standalone。
xmlVersion 设置或返回文档的 XML 版本。

Document Object

方法 描述
adoptNode(sourcenode) 从另一个文档向本文档选定一个节点,然后返回被选节点。
createAttribute(name) 创建带有指定名称的属性节点,并返回新的 Attr 对象。
createAttributeNS(uri,name) 创建带有指定名称和命名空间的属性节点,并返回新的 Attr 对象。
createCDATASection() 创建 CDATA 区段节点。
createComment() 创建注释节点。
createDocumentFragment() 创建空的 DocumentFragment 对象,并返回此对象。
createElement() 创建元素节点。
createElementNS() 创建带有指定命名空间的元素节点。
createEntityReference(name) 创建 EntityReference 对象,并返回此对象。
createProcessingInstruction(target,data) 创建一个 ProcessingInstruction 对象,并返回此对象。
createTextNode() 创建文本节点。
getElementById(id) 返回带有指定值的 ID 属性的元素。如果不存在这样的元素,则返回 null。
getElementsByTagName() 返回带有指定名称的所有元素的 NodeList。
getElementsByTagNameNS() 返回带有指定名称和命名空间的所有元素的 NodeList。
importNode(nodetoimport,deep) 从另一个文档向本文档选定一个节点。该方法创建源节点的一个新的副本。如果 deep 参数设置为 true,它将导入指定节点的所有子节点。 如果设置为 false,它将只导入节点本身。该方法返回被导入的节点。
normalizeDocument()
renameNode() 重命名元素或属性节点。