Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM - Element object


May 27, 2021 XML DOM


Table of contents


XML DOM - Element object


Element object

The Element object represents an element in the XML document. E lements can contain properties, other elements, or text. If an element contains text, it is represented in the text node.

Important: Text is always stored in the text node. A common mistake in DOM processing is to navigate to an element node and think it contains text. H owever, even the simplest element nodes have text nodes. For example, in slt;year and 2005, there is an element node (year), and there is a text node below that contains text (2005).

Because an Element object is also a node, it inherits the properties and methods of the Node object.

Element object properties

属性 描述
attributes 返回元素的属性的 NamedNodeMap。
baseURI 返回元素的绝对基准 URI。
childNodes 返回元素的子节点的 NodeList。
firstChild 返回元素的第一个子节点。
lastChild 返回元素的最后一个子节点。
localName 返回元素名称的本地部分。
namespaceURI 返回元素的命名空间 URI。
nextSibling 返回元素之后紧接的节点。
nodeName 返回节点的名称,根据其类型。
nodeType 返回节点的类型。
ownerDocument 返回元素所属的根元素 (document 对象)。
parentNode 返回元素的父节点。
prefix 设置或返回元素的命名空间前缀。
previousSibling 返回元素之前紧接的节点。
schemaTypeInfo 返回与元素相关联的类型信息。
tagName 返回元素的名称。
textContent 设置或返回元素及其后代的文本内容。

Element object method

方法 描述
appendChild() 把新的子节点添加到节点的子节点列表末尾。
cloneNode() 克隆节点。
compareDocumentPosition() 比较两个节点的文档位置。
getAttribute() 返回属性的值。
getAttributeNS() 返回属性的值(带有命名空间)。
getAttributeNode() 以 Attribute 对象返回属性节点。
getAttributeNodeNS() 以 Attribute 对象返回属性节点(带有命名空间)。
getElementsByTagName() 返回匹配的元素节点及它们的子节点的 NodeList。
getElementsByTagNameNS() 返回匹配的元素节点(带有命名空间)及它们的子节点的 NodeList。
getFeature(feature,version) 返回 DOM 对象,此对象可执行带有指定特性和版本的专门的 API。
getUserData(key) 返回与节点上键关联的对象。此对象必须首先通过使用相同的键调用 setUserData 来设置到此节点。
hasAttribute() 返回元素是否拥有匹配指定名称的属性。
hasAttributeNS() 返回元素是否拥有匹配指定名称和命名空间的属性。
hasAttributes() 返回元素是否拥有属性。
hasChildNodes() 返回元素是否拥有子节点。
insertBefore() 在已有的子节点之前插入一个新的子节点。
isDefaultNamespace(URI) 返回指定的 namespaceURI 是否为默认。
isEqualNode() 检查两个节点是否相等。
isSameNode() 检查两个节点是否为同一节点。
isSupported(feature,version) 返回指定的特性是否在此元素上得到支持。
lookupNamespaceURI() 返回匹配指定前缀的命名空间 URI。
lookupPrefix() 返回匹配指定命名空间 URI 的前缀。
normalize() 把节点(包括属性)下的所有文本节点放置到一个"标准"的格式中,其中只有结构(比如元素、注释、处理指令、CDATA 区段以及实体引用)来分隔 Text 节点,例如,既没有相邻的 Text 节点,也没有空的 Text 节点。
removeAttribute() 删除指定的属性。
removeAttributeNS() 删除指定的属性(带有命名空间)。
removeAttributeNode() 删除指定的属性节点。
removeChild() 删除子节点。
replaceChild() 替换子节点。
setUserData(key,data,handler) 把对象关联到元素上的键。
setAttribute() 添加新属性。
setAttributeNS() 添加新属性(带有命名空间)。
setAttributeNode() 添加新的属性节点。
setAttributeNodeNS(attrnode) 添加新的属性节点(带有命名空间)。
setIdAttribute(name,isId) 如果 Attribute 对象的 isId 属性为 true,那么此方法会把指定的属性声明为一个用户确定 ID 的属性(user-determined ID attribute)。
setIdAttributeNS(uri,name,isId) 如果 Attribute 对象的 isId 属性为 true,那么此方法会把指定的属性声明为一个用户确定 ID 的属性(user-determined ID attribute)(带有命名空间)。
setIdAttributeNode(idAttr,isId) 如果 Attribute 对象的 isId 属性为 true,那么此方法会把指定的属性声明为一个用户确定 ID 的属性(user-determined ID attribute)。

Related articles

XML DOM Node object