XML DOM - Element Object
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 mistake in DOM processing is navigating to an element node and assuming it contains text. However, even the simplest element node has a text node beneath it. For example, in
Since an Element object is also a node, it inherits 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, according to 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 | Returns the 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
| Method | Description |
|---|---|
| appendChild() | Adds a new child node to the end of the node's list of child nodes. |
| cloneNode() | Clones the node. |
| compareDocumentPosition() | Compares the document positions of two nodes. |
| getAttribute() | Returns the value of an attribute. |
| getAttributeNS() | Returns the value of an attribute (with namespace). |
| getAttributeNode() | Returns an attribute node as an Attribute object. |
| getAttributeNodeNS() | Returns an 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 perform specialized APIs with the specified feature and version. |
| getUserData(key) | Returns the object associated with a key on the node. This object must first have been set on the 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 any attributes. |
| hasChildNodes() | Returns whether the element has any 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() | Puts all text nodes under the node (including attributes) into a "normal" format in which only structure (such as elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, that is, there are neither adjacent Text nodes nor empty Text nodes. |
| removeAttribute() | Removes the specified attribute. |
| removeAttributeNS() | Removes the specified attribute (with namespace). |
| removeAttributeNode() | Removes the specified attribute node. |
| removeChild() | Remove a child node. |
| replaceChild() | Replace a child node. |
| setUserData(key,data,handler) | Associates an object with a key on the element. |
| setAttribute() | Adds 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. |