- Defining the Document Object Model
- DOM Core Level I
- Creating Document Objects
- Node Interface
- NodeList and NamedNodeMap
- Document Interface
- Element Interface
- Attr Interface
- Additional Interfaces
- Creating DOM Elements
- DOM Level II
- The DOM Core Defined
- Implementation Anomalies
- Suggested for Further Study
- Further Reading
We introduced the Attribute interface in the previous section, and we saw the use of the Attribute interface in Listing 3.5. We should now examine the Attribute interface in detail. Attributes, represented by the java interface Attr, allow for creating, examining, and otherwise manipulating the XML representation of attributes via the DOM. Attributes are not part of the DOM tree per se. They are never children of any given node but must be acted on specifically by calling one of the get/set attribute methods on an element. As a result, getPreviousSibling, getParent, and getNextSibling methods return null. In short, attributes are properties of nodes, are owned by a node, but are not children of nodes. One further notewhile Attributes are never children of a node, an Attribute, which contains EntityReferences, can have children.
The Attr interface contains only four methods. Note that one additional method exists in the Attr interfacegetSpecified().
String getName() Returns the name of the attribute. For example, discount or cur.
String getValue() Returns the value of the attribute. Entity references and character data are replaced with their appropriate expanded values.
String setValue(String newValue) Replaces the value of the Attr with the value specified by newValue.
boolean getSpecified() Returns true or false defined as follows:
true The attribute was given a value in the original document.
true The user set the value of the attribute programmatically.
false The attribute was specified in the DTD but not given a value; this is the default value.
false The attribute has no assigned value but is #IMPLIED in the DTD.