Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Attr Interface

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 note—while 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 interface—getSpecified().

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

  • + Share This
  • 🔖 Save To Your Account