XML DOM - Element object

# XML DOM - The Element Object

« Previous
Next Chapter »

## 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.

## Element Object Properties

Property | Description
--- | ---
attributes | Returns a NamedNodeMap of attributes for the element
baseURI | Returns the absolute base URI of the element
childNodes | Returns a NodeList of child nodes for the element
firstChild | Returns the first child of the element
lastChild | Returns the last child of the element
localName | Returns the local part of the name of the element
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) for an element
parentNode | Returns the parent node of the element
prefix | Sets or returns the namespace prefix of the element
previousSibling | Returns the node immediately before 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 list of children of the node
cloneNode() | Clones a node
compareDocumentPosition() | Compares the document position of two nodes
getAttribute() | Returns the value of an attribute
getAttributeNS() | Returns the value of an attribute (with a namespace)
getAttributeNode() | Returns an attribute node as an Attribute object
getAttributeNodeNS() | Returns an attribute node (with a namespace) as an Attribute object
getElementsByTagName() | Returns a NodeList of matching element nodes, and their children
getElementsByTagNameNS() | Returns a NodeList of matching element nodes (with a namespace), and their children
getFeature(feature,version) | Returns a DOM object which implements the specialized APIs of the specified feature and version
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
hasAttribute() | Returns whether an element has any attributes matching a specified name
hasAttributeNS() | Returns whether an element has any attributes matching a 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 if two nodes are equal
isSameNode() | Checks if two nodes are the same node
isSupported(feature,version) | Returns whether a specified feature is supported on the element
lookupNamespaceURI() | Returns the namespace URI matching a specified prefix
lookupPrefix() | Returns the prefix matching a specified namespace URI
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 nodes
removeAttribute() | Removes a specified attribute
removeAttributeNS() | Removes a specified attribute (with a namespace)
removeAttributeNode() | Removes a specified attribute node
removeChild() | Removes a child node
replaceChild() | Replaces a child node
setUserData(key,data,handler) | Associates an object to a key on the element
setAttribute() | Adds a new attribute
setAttributeNS() | Adds a new attribute (with a namespace)
setAttributeNode() | Adds a new attribute node
setAttributeNodeNS(attrnode) | Adds a new attribute node (with a namespace)
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
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
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

« Previous
Next Chapter »

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples