XML DOM - The Element Object

The Element object

The Element object represents an element in an XML document. Elements may contain attributes, other elements, or text. If an element contains text, the text is represented in a text-node.

IMPORTANT! Text is always stored in text nodes. A common error in DOM processing is to navigate to an element node and expect it to contain the text. However, even the simplest element node has a text node under it. For example, in <year>2005</year>, there is an element node (year), and a text node under it, which contains the text (2005).

Because the Element object is also a Node, it inherits the Node object's properties and methods.

IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet Standard)

Element Object Properties

PropertyDescriptionIEFOW3C
attributesReturns a NamedNodeMap of attributes for the element519Yes
baseURIReturns the absolute base URI of the elementNo1NoYes
childNodesReturns a NodeList of child nodes for the element519Yes
firstChildReturns the first child of the element519Yes
lastChildReturns the last child of the element519Yes
localNameReturns the local part of the name of the elementNo19Yes
namespaceURIReturns the namespace URI of the elementNo19Yes
nextSiblingReturns the node immediately following the element519Yes
nodeNameReturns the name of the node, depending on its type519Yes
nodeTypeReturns the type of the node519Yes
ownerDocumentReturns the root element (document object) for an element519Yes
parentNodeReturns the parent node of the element519Yes
prefixSets or returns the namespace prefix of the elementNo19Yes
previousSiblingReturns the node immediately before the element519Yes
schemaTypeInfoReturns the type information associated with the element  NoYes
tagNameReturns the name of the element519Yes
textContentSets or returns the text content of the element and its descendantsNo1NoYes
textReturns the text of the node and its descendants. IE-only property5NoNoNo
xmlReturns the XML of the node and its descendants. IE-only property5NoNoNo

Element Object Methods

MethodDescriptionIEFOW3C
appendChild()Adds a new child node to the end of the list of children of the node519Yes
cloneNode()Clones a  node519Yes
compareDocumentPosition()Compares the document position of two nodesNo1NoYes
getAttribute()Returns the value of an attribute519Yes
getAttributeNS()Returns the value of an attribute (with a namespace)No19Yes
getAttributeNode()Returns an attribute node as an Attribute object519Yes
getAttributeNodeNS()Returns an attribute node (with a namespace) as an Attribute objectNo 9Yes
getElementsByTagName()Returns a NodeList of matching element nodes, and their  children519Yes
getElementsByTagNameNS()Returns a NodeList of matching element nodes (with a namespace), and their  childrenNo19Yes
getFeature(feature,version)Returns a DOM object which implements the specialized APIs of the specified feature and version  NoYes
getUserData(key)Returns the object associated to a key on a this node. The object must first have been set to this node by calling setUserData with the same key  NoYes
hasAttribute()Returns whether an element has any attributes matching a specified name519Yes
hasAttributeNS()Returns whether an element has any attributes matching a specified name and namespaceNo19Yes
hasAttributes()Returns whether the element has any attributes519Yes
hasChildNodes()Returns whether the element has any child nodes519Yes
insertBefore()Inserts a new child node before an existing child node519Yes
isDefaultNamespace(URI)Returns whether the specified namespaceURI is the default  NoYes
isEqualNode()Checks if two nodes are equalNoNoNoYes
isSameNode()Checks if two nodes are the same nodeNo1NoYes
isSupported(feature,version)Returns whether a specified feature is supported on the element  9Yes
lookupNamespaceURI()Returns the namespace URI matching a specified prefixNo1NoYes
lookupPrefix()Returns the prefix matching a specified namespace URINo1NoYes
normalize()Puts all text nodes underneath this element (including attributes) into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes519Yes
removeAttribute()Removes a specified attribute519Yes
removeAttributeNS()Removes a specified attribute (with a namespace)No19Yes
removeAttributeNode()Removes a specified attribute node519Yes
removeChild()Removes a child node519Yes
replaceChild()Replaces a child node519Yes
setUserData(key,data,handler)Associates an object to a key on the element  NoYes
setAttribute()Adds a new attribute519Yes
setAttributeNS()Adds a new attribute (with a namespace) 19Yes
setAttributeNode()Adds a new attribute node519Yes
setAttributeNodeNS(attrnode)Adds a new attribute node (with a namespace)  9Yes
setIdAttribute(name,isId)If the isId property of the Attribute object is true, this method declares the specified attribute to be a user-determined ID attribute  NoYes
setIdAttributeNS(uri,name,isId)If the isId property of the Attribute object is true, this method declares the specified attribute (with a namespace) to be a user-determined ID attribute  NoYes
setIdAttributeNode(idAttr,isId)If the isId property of the Attribute object is true, this method declares the specified attribute to be a user-determined ID attribute  NoYes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值