Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 1097
نمايش فراداده

Attrorg.w3c.dom

Java 1.4

An Attr object represents an attribute of an Element node. Attr objects are associated with Element nodes, but are not directly part of the document tree: the getParentNode( ) method of an Attr object always returns null. Use getOwnerElement( ) to deterine which Element an Attr is part of. You can obtain an Attr object by calling the getAttributeNode( ) method of Element, or you can obtain a NamedNodeMap of all Attr objects for an element with the getAttributes( ) method of Node.

getName( ) returns the name of the attribute. getValue( ) returns the attribute value as a string. getSpecified( ) returns TRue if the attribute was explicitly specified in the source document through a call to setValue( ), and returns false if the attribute represents a default obtained from a DTD or other schema.

XML allows attributes to contain text and entity references. The getValue( ) method returns the attribute value as a single string. If you want to know the precise composition of the attribute however, you can examine the children of the Attr node: they may consist of Text and/or EntityReference nodes.

In most cases the easiest way to work with attributes is with the getAttribute( ) and setAttribute( ) methods of the Element interface. These methods avoid the use of Attr nodes altogether.

Figure 21-1. org.w3c.dom.Attr

public interface

Attr extends Node { // Public Instance Methods String

getName ( ); Element

getOwnerElement ( );

5.0 TypeInfo

getSchemaTypeInfo ( ); boolean

getSpecified ( ); String

getValue ( );

5.0 boolean

isId ( ); void

setValue (String

value ) throws DOMException; }

Passed To

Element.{removeAttributeNode( ), setAttributeNode( ), setAttributeNodeNS( ), setIdAttributeNode( )}

Returned By

Document.{createAttribute( ), createAttributeNS( )}, Element.{getAttributeNode( ), getAttributeNodeNS( ), removeAttributeNode( ), setAttributeNode( ), setAttributeNodeNS( )}