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

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

The interface Attr represents a single XML attribute of an XML element. The values of parentNode, previousSibling and nextSibling of an Attr are always null. Although the Attr interface derives from the Node interface, many methods of the base interface Node are not applicable.

An XML element can acquire an XML attribute in several ways. It has an attribute value when:

An Attr node can have EntityReference objects as child nodes. The attribute value provides an expanded DOMString representation of the attribute.

//Lies das XML-Attribut size des Elements als Attr-Objekt
Attr attrName = elem.getAttributeNode("size");

 

Attributes

An Attr object has the following attributes:

 

isId: boolean(3)

Returns true, if this attribute contains a unique identifier for its parent element node. Attributes are marked as identifiers either by the DTD or by calling the method setIdAttribute( ) of the interface Element. Read-only.

Java binding
public boolean isId( );

 

name: DOMString

The attribute name; read-only.

Java binding
public String getName( );

Java example

// Ausgabe der Namen aller XML-Attribute des Elements
Attr attr;

for (int i = 0; i < elem.getAttribute().getLength(  ); i++) {
    // Temporärer Alias für das Attribut
    attr = (Attr)elem.getAttribute(  ).item(i);
    System.out.println(attr.getName(  ));
    }

 

ownerElement: Element(2)

Contains a reference to the Element object to which this attribute belongs. If it currently belongs to no element, the value is null. Read-only.

Java binding
public Element getOwnerElement( );

 

schemaTypeInfo: TypeInfo(3)

This property links to any type information available for this attribute, based on the DTD or schema associated with the parent document. It may be unreliable if the node has been moved between elements. Read-only.

Java binding
public TypeInfo getSchemaTypeInfo( );

 

specified: Boolean value

Indicates whether an attribute was explicitly specified in the XML document or taken as a default value from the DTD or schema. Read-only.

Java binding
public boolean getSpecified( );

Java example

// Ausgabe der Namen aller XML-Attribute des Elements
for (int i = 0; i < elem.getAttribute().getLength(  ); i++) {
    // Temporärer Alias für das Attribut
    attr = (Attr)elem.getAttribute(  ).item(i);
    // Nur diejenigen Attribute werden ausgegeben, die im
    //gelesenen XML-Dokument explizit angegeben wurden.
     // (D.h., wir ignorieren aus der DTD übernommene Default-Werte.)
     if (attr.getSpecified(  )) {
         System.out.println(attr.getName(  ));
    }
}

 

 

value: DOMString

This attribute provides a convenient way to read or change the textual value of the Attr object. Reading the attribute returns the value produced by the parser after replacing any entity references. Setting the value creates a child node of type Text containing the value as a string. Some Attr nodes are read-only. Trying to change their value raises a DOM exception NO_MODIFICATION_ALLOWED_ERR.

Java bindings
public String getValue( ); public void setValue(String value);

Java example

// Alle Attributwerte in Kleinbuchstaben umwandeln
Attr attr;

for (int i = 0; i < elem.getAttribute().getLength(  ); i++) {
    attr = (Attr)elem.getAttribute(  ).item(i);
attr.setValue(attr.getValue().toLowerCase(  ));
}

 

 

Methods

The object Attr has no methods.

  

 

 

 

back to top