XML DOM - ElementObject


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, that text is represented in a text node.

Important:Text is always stored in text nodes. A common error in DOM processing is navigating to an element node and assuming that this node contains text. However, even the simplest element node has a text node beneath 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.

Element Object Properties

Property Description
attributes Returns a NamedNodeMap of the element's attributes.
baseURI Returns the absolute base URI of the element.
childNodes Returns a NodeList of the element's child nodes.
firstChild Returns the first child node of the element.
lastChild Returns the last child node of the element.
localName Returns the local part of the element's name.
namespaceURI Returns the namespace URI of the element.
nextSibling Returns the node immediately following the element.
nodeName Returns the name of the node, depending on its type.
nodeType Returns the type of the node.
ownerDocument Returns the root element (document object) to which the element belongs.
parentNode Returns the parent node of the element.
prefix Sets or returns the namespace prefix of the element.
previousSibling Returns the node immediately preceding the element.
schemaTypeInfo Return type information associated with the element.
tagName Returns the name of the element.
textContent Sets or returns the text content of the element and its descendants.

Element Object Methods

Methods Description
appendChild() Adds a new child node to the end of the node's list of children.
cloneNode() Clone node.
compareDocumentPosition() Compares the document position of two nodes.
getAttribute() Return the value of the attribute.
getAttributeNS() Returns the value of the attribute (with namespace).
getAttributeNode() Returns the attribute node as an Attribute object.
getAttributeNodeNS() Returns the attribute node as an Attribute object (with namespace).
getElementsByTagName() Returns a NodeList of matching element nodes and their child nodes.
getElementsByTagNameNS() Returns a NodeList of matching element nodes (with namespace) and their child nodes.
getFeature(feature,version) Returns a DOM object that can execute a specialized API with the specified features and version.
getUserData(key) Returns the object associated with a key on the node. This object must first have been set to this node by calling setUserData with the same key.
hasAttribute() Returns whether the element has an attribute matching the specified name.
hasAttributeNS() Returns whether the element has an attribute matching the specified name and namespace.
hasAttributes() Returns whether the element has attributes.
hasChildNodes() Returns whether the element has child nodes.
insertBefore() Inserts a new child node before an existing child node.
isDefaultNamespace(URI) Returns whether the specified namespaceURI is the default.
isEqualNode() Checks whether two nodes are equal.
isSameNode() Checks whether two nodes are the same node.
isSupported(feature,version) Returns whether the specified feature is supported on this element.
lookupNamespaceURI() Returns the namespace URI matching the specified prefix.
lookupPrefix() Returns the prefix matching the specified namespace URI.
normalize() Places all text nodes under the node (including attributes) into a "normal" form, where only structure (such as elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, for example, neither adjacent Text nodes nor empty Text nodes exist.
removeAttribute() Removes the specified attribute.
removeAttributeNS() Removes the specified attribute (with namespace).
removeAttributeNode() Removes the specified attribute node.
removeChild() Remove the child node.
replaceChild() Replace the child node.
setUserData(key,data,handler) Associates an object to a key on the element.
setAttribute() Add a new attribute.
setAttributeNS() Adds a new attribute (with namespace).
setAttributeNode() Adds a new attribute node.
setAttributeNodeNS(attrnode) Adds a new attribute node (with namespace).
setIdAttribute(name,isId) If the isId property of the Attribute object is true, this method declares the specified attribute as a user-determined ID attribute.
setIdAttributeNS(uri,name,isId) If the isId property of the Attribute object is true, this method declares the specified attribute as a user-determined ID attribute (with namespace).
setIdAttributeNode(idAttr,isId) If the isId property of the Attribute object is true, this method declares the specified attribute as a user-determined ID attribute.

other extensions