XML DOM - Element Object

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

Element object

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

Important note:Important note: Text is always stored in text nodes. A common mistake in DOM processing is to navigate to an element node and assume that this node contains text. However, even the simplest element node has a text node below it. For example, in <year>2005</year>, there is an element node (year), and below this node there is a text node containing the text (2005).

Since the element object is also a node, it can inherit the properties and methods of the Node object.

Properties of Element objects

Properties Description IE F O W3C
attributes Returns the NamedNodeMap of the element's attributes 5 1 9 Yes
baseURI Returns the absolute base URI of the element No 1 No Yes
childNodes Returns the NodeList of the element's child nodes 5 1 9 Yes
firstChild Returns the first child of the element 5 1 9 Yes
lastChild Returns the last child of the element 5 1 9 Yes
localName Returns the local part of the element name No 1 9 Yes
namespaceURI Returns the namespace URI of the element No 1 9 Yes
nextSibling Returns the node immediately following the element 5 1 9 Yes
nodeName Returns the name of the node, based on its type. 5 1 9 Yes
nodeType Returns the type of the node 5 1 9 Yes
ownerDocument Returns the root element of the element (document object) 5 1 9 Yes
parentNode Returns the parent node of the element 5 1 9 Yes
prefix Sets or returns the namespace prefix of the element No 1 9 Yes
previousSibling Returns the node immediately preceding the element 5 1 9 Yes
schemaTypeInfo Returns the type information associated with the element     No Yes
tagName Returns the name of the element 5 1 9 Yes
textContent Sets or returns the text content of the element and its descendants No 1 No Yes
text Returns the text of the node and its descendants (IE-only) 5 No No No
xml Returns the XML of the node and its descendants (IE-only) 5 No No No

Methods of Element objects

Method Description IE F O W3C
appendChild() Add a new child node to the end of the node's child node list. 5 1 9 Yes
cloneNode() Clone a node. 5 1 9 Yes
compareDocumentPosition() Compare the document positions of two nodes. No 1 No Yes
dispatchEvent() Assign a synthetic event to the node. No 1 9 Yes
getAttribute() Return the value of the attribute. 5 1 9 Yes
getAttributeNS() Return the value of the attribute. No 1 9 Yes
getAttributeNode() Return the attribute node as an Attribute object. 5 1 9 Yes
getAttributeNodeNS() Return the attribute node as an Attribute object. No   9 Yes
getElementsByTagName() Find the descendant element with the specified tag name. 5 1 9 Yes
getElementsByTagNameNS() Find the element with the specified tag name and namespace. No 1 9 Yes
getFeature(feature,version) Return a DOM object that can execute specialized APIs with the specified features and versions.     No Yes
getUserData(key) Return the object associated with the key on the linked node. This object must first be set to this node by calling setUserData with the same key.     No Yes
hasAttribute() Return whether the element has the specified attribute. 5 1 9 Yes
hasAttributeNS() Return whether the element has the specified attribute. No 1 9 Yes
hasAttributes() Return whether the element has an attribute. 5 1 9 Yes
hasChildNodes() Return whether the element has a child node. 5 1 9 Yes
insertBefore() Insert a new child node before an existing child node. 5 1 9 Yes
isDefaultNamespace(URI) Return whether the specified namespace URI is the default.     No Yes
isEqualNode() Check if two nodes are equal. No No No Yes
isSameNode() Check if two nodes are the same node. No 1 No Yes
isSupported(feature,version) Return whether the specified feature is supported on this element.     9 Yes
lookupNamespaceURI() Return the namespace URI that matches the specified prefix. No 1 No Yes
lookupPrefix() Return the prefix that matches the specified namespace URI. No 1 No Yes
normalize()   5 1 9 Yes
removeAttribute() Remove the specified attribute. 5 1 9 Yes
removeAttributeNS() Remove the specified attribute. No 1 9 Yes
removeAttributeNode() Remove the specified attribute node. 5 1 9 Yes
removeChild() Remove the child node. 5 1 9 Yes
replaceChild() Replace the child node. 5 1 9 Yes
setUserData(key,data,handler) Associate an object with a key on the element.     No Yes
setAttribute() Add a new attribute. 5 1 9 Yes
setAttributeNS() Add a new attribute.   1 9 Yes
setAttributeNode() Add a new attribute node. 5 1 9 Yes
setAttributeNodeNS(attrnode) Add a new attribute node.     9 Yes
setIdAttribute(name,isId) If the isId property of the Attribute object is true, then this method will declare the specified attribute as a user-determined ID attribute (user-determined ID attribute).     No Yes
setIdAttributeNS(uri,name,isId) If the isId property of the Attribute object is true, then this method will declare the specified attribute as a user-determined ID attribute (user-determined ID attribute) (with namespace).     No Yes
setIdAttributeNode(idAttr,isId) If the isId property of the Attribute object is true, then this method will declare the specified attribute as a user-determined ID attribute (user-determined ID attribute).     No Yes