XML technologies / XML / XML in a Nutshell / DOM reference / Object reference / Element

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

The interface Element provides access to XML structure and data. Each XML element maps to exactly one Element node, including the document element, accessible through the attribute documentElement of the Document object. From there, traverse the tree to manipulate the entire structure.

Java example

// Lies das Dokumentelement des Dokuments.
Element elem = doc.getDocumentElement(  );

This extends the Node interface with access to XML attributes. Two variants use either Attr object trees or simple DOMStrings.

 

Attributes

The object Element has these attributes:

 

schemaTypeInfo: TypeInfo (3)

Links to any type information available for this document from a DTD or schema associated with its parent document. Read-only.

Java binding
public TypeInfo getSchemaTypeInfo( );

 

tagName: DOMString

The XML tag name in the original document.

Java binding
public String getTagName( );

Java example

// Gib den Namen des Dokumentelements aus.
Element elem = doc.getDocumentElement(  );
System.out.println("Das ist ein " + elem.getTagName(  ) + " Dokument.");

 

Methods

These methods are defined for this object:

 

getAttribute: name

Returns the named attribute’s value as a DOMString. For details on how the value is determined, see “getAttributeNode: name”. If neither explicitly set nor defaulted in the DTD, returns an empty string.

Java binding
public String getAttribute(String name);

Java example

// Prüfe, ob das Attribut name gesetzt ist.
Element elem = doc.getDocumentElement(  );

if (elem.getAttribute("name") == "") {
    System.out.println("warning: " + elem.getTagName(  ) +
                   " element: no name attribute");
}

 

getAttributeNS: namespaceURI, localName (2)

Returns an attribute value as a DOMString, but supports namespaces, taking the URI and local part of the qualified name.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the attribute to read.

  • localName: DOMString

    Local part of the qualified attribute name to read.

Return value

An empty string if the attribute is neither explicitly set nor given a default by the DTD.

Java binding
public String getAttributeNS(String namespaceURI, String localName);

 

getAttributeNode: name

Returns an Attr object whose attribute name has the supplied value, or null if no matching object exists.

Argument

  • name: DOMString

    Name of the attribute to read.

Java binding
public Attr getAttributeNode(String name);

Java example

// Benutze das id-Attribut.
Attr attr;
if ((attr = elem.getAttributeNode("id")) == null) {
    System.out.println("Warnung: Das Element " + elem.getTagName(  ) +
                       ": enthält kein id-Attribut.");
}

 

getAttributeNodeNS: namespaceURI, localName (2)

Returns an Attr object matching the supplied namespace URI and local name, or null if none exists.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the desired attribute.

  • localName: DOMString

    Local name: the part after : in a fully qualified attribute name.

Java binding
public Attr getAttributeNodeNS(String namespaceURI, String localName);

 

getElementsByTagName: name

Returns a NodeList containing all Element nodes whose attribute tagName matches the parameter name. Nodes are returned in recursive tree order: children first, then following siblings.

Argument

  • name: DOMString

    Tag name to find. The special name * matches any tag.

Java binding
public NodeList getElementsByTagName(String name);

Java example

// Generiere eine Liste aller Adresseinträge im aktuellen Dokument.
Element elem = doc.getDocumentElement(  );
NodeList nlAddrs = elem.getElementsByTagName("adresse");

 

getElementsByTagNameNS: namespaceURI, localName (2)

Corresponds to the method getElementsByTagName. Returns a list of all Element nodes and descendants matching the criteria supplied in namespaceURI and localName. Matching elements are listed recursively, visiting each node’s children before its siblings.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the desired elements. * matches any namespace.

  • localName: DOMString

    Local name of the desired elements. * matches any local name.

Java binding
public NodeList getElementsByTagNameNS(String namespaceURI, String localName);

 

hasAttribute: name (2)

Returns true if the named attribute is explicitly specified or defaulted in the DTD; otherwise returns false.

Argument

  • name: DOMString

    Name of the desired attribute.

Java binding
public boolean hasAttribute(String name);

 

hasAttributeNS: namespaceURI, localName (2)

Returns true if the element has an attribute with namespace URI namespaceURI and local name localName; otherwise false. It does not matter whether the attribute is explicit or supplied by a DTD default.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the desired attribute.

  • localName: DOMString

    Local name of the desired attribute.

Java binding
public boolean hasAttribute(String namespaceURI, String localName);

 

normalize

Traverses the children of the current Element object, merging consecutive Text nodes into one.

Note: In DOM Level 2, this method moved from the interface Element to Node. Elements are nodes, so it remains applicable to elements and the change is backward compatible.

Java binding
public void normalize( );

Java example

// Alle aufeinander folgenden Textknoten werden verschmolzen.
elem.normalize(  );

 

removeAttribute: name

^

Removes the named attribute. If the DTD specifies a default, subsequent reads return that default.

Argument

  • name: DOMString

    Name of the attribute to remove.

Exception

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

Java binding
public void removeAttribute(String name) throws DOMException;

Java example

// Lösche die eindeutige id.
...
elem.removeAttribute("id");
...

 

removeAttributeNS: namespaceURI, localName (2)

Removes the attribute identified by namespace URI and local name from the element’s attribute list.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the attribute to remove.

  • localName: DOMString

    Local name to remove: the part after : in a qualified name.

Exception

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

Java binding
public void removeAttributeNS(String namespaceURI, String localName) throws DOMException;

 

removeAttributeNode: oldAttr

Removes the supplied attribute node from the element’s attributes list. If the DTD supplies a default, subsequent reads return it.

Argument

  • oldAttr: Attr

    Attribute node to remove.

Exceptions

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

  • NOT_FOUND_ERR

    Raised if no attribute matching the parameter oldAttr exists in the element’s attribute list.

Java binding
public Attr removeAttributeNode(Attr oldAttr) throws DOMException;

Java example

// Finde und lösche temporäre Attribute.
Attr attr;

if ((attr = elem.getAttributeNode("temp")) != null) {
    // Lösche das Attribut.
    elem.removeAttributeNode(attr);
}

 

setAttribute: name, value

Sets the attribute named by name to the value supplied as a DOMString in the argument value. The string is not checked for entity references. A Text child holding the value is simply added to the corresponding element of the attributes list. If a same-named node exists, its value argument is modified.

Arguments

  • name: DOMString

    Name of the attribute to set or change.

  • value: DOMString

    New attribute value.

Exceptions

  • INVALID_CHARACTER_ERR

    The supplied name is not a valid XML attribute name.

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

Java binding
public void setAttribute(String name, String value) throws DOMException;

Java example

// Prüfe, ob ein Attribut name vorhanden ist.
if (elem.getAttribute("name") == "") {
    // Lege einen sinnvollen Default-Wert fest.
    elem.setAttribute("name", elem.getTagName(  ));
}

 

setAttributeNS: namespaceURI, qualifiedName, value (2)

Corresponds to the method setAttribute but supports namespaces. The namespace URI and qualified name are updated in the selected element’s attributes list.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the attribute to find or create.

  • qualifiedName: DOMString

    Fully qualified name, including prefix, of the attribute to find or create.

  • value: DOMString

    New attribute value.

Exceptions

  • INVALID_CHARACTER_ERR

    The supplied name is not a valid XML attribute name.

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

  • NAMESPACE_ERR

    Raised if parameters namespaceURI and qualifiedName violate namespace rules. A qualified name with a prefix requires a namespace URI other than null or an empty string. Reserved prefixes xml or xmlns require the corresponding system URI. See Namespaces.

Java binding
public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

 

setAttributeNode: newAttr

Sets or replaces an attribute in the attributes list of the Node using the supplied Attr object. Its name is read from the new attribute object’s name attribute. If an Attr object with that name already exists in the attributes list, it is replaced and a reference to the old Attr object is returned. Otherwise returns null.

Argument

  • newAttr: Attr

    The new Attr object to set.

Exceptions

  • WRONG_DOCUMENT_ERR

    Raised if the newAttr node belongs to a different document from its parent node.

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the new parent is read-only.

  • INUSE_ATTRIBUTE_ERR

    Raised if another Element already uses the new Attr node. Each element needs its own Attr object.

Java binding
public Attr setAttributeNode(Attr newAttr) throws DOMException;

Java example

// Stelle sicher, dass ein id-Attribut vorhanden ist.
Attr attr;

if ((attr = elem.getAttributeNode("id")) == null) {
    // Generiere eine neue, eindeutige id.
    attr = doc.createAttribute("id");

    elem.setAttributeNode(attr);

    // Setze die Verarbeitung fort.
}

 

setAttributeNodeNS: newAttr (2)

Sets or replaces attributes in the element’s attributes list, searching by the supplied Attr object’s namespace URI and local name. Corresponds to setAttributeNode but respects attribute namespaces. If an Attr object with that name already exists in the attributes list, it is replaced and a reference to the old Attr object is returned. Otherwise returns null.

Argument

  • newAttr: Attr

    The new Attr object to set.

Exceptions

  • WRONG_DOCUMENT_ERR

    Raised if the newAttr node belongs to a different document from its parent node.

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the new parent is read-only.

  • INUSE_ATTRIBUTE_ERR

    Raised if another Element already uses the new Attr node. Each element needs its own Attr object.

Java binding
public Attr setAttributeNodeNS(Attr newAttr) throws DOMException;

 

setIdAttribute: name, isId (3)

Marks an attribute as a user-defined ID attribute. Such attributes appear in searches with Document.getElementById( ) without affecting type information supplied by Attr.schemaTypeInfo. Use setIdAttributeNS( ) for namespaced attributes.

Arguments

  • name: DOMString

    Name of the attribute to modify.

  • isId: boolean

    Set to true to make it an ID attribute; otherwise set to false.

Exceptions

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

  • NOT_FOUND_ERR

    Raised if the attribute collection has no attribute matching the parameter name.

Java binding
public void setIdAttribute(String name, boolean isId) throws DOMException;

 

setIdAttributeNS: namespaceURI, localName, isId (3)

Marks a namespaced attribute as a user-defined ID. It appears in searches with Document.getElementById( ) without affecting type information supplied by Attr.schemaTypeInfo.

Arguments

  • namespaceURI: DOMString

    Namespace URI of the attribute to modify.

  • localName: DOMString

    Local part of the name of the attribute to modify.

  • isId: boolean

    Set to true to make it an ID attribute; otherwise set to false.

Exceptions

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

  • NOT_FOUND_ERR

    Raised if the attribute collection has no attribute matching the parameter name.

Java binding
public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException;

 

setIdAttributeNode: idAttr, isId (3)

Marks an attribute as a user-defined ID. It appears in searches with Document.getElementById( ) without affecting type information supplied by Attr.schemaTypeInfo. Use setIdAttributeNS( ) for namespaced attributes.

Arguments

  • idAttr: Attr

    Attribute to modify.

  • isId: boolean

    Set to true to make it an ID attribute; otherwise set to false.

Exceptions

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the element is read-only.

  • NOT_FOUND_ERR

    Raised if the attribute collection has no attribute matching the parameter name.

Java binding
public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException;

  

 

 

 

back to top