Top Description Fields Constructors Methods
org.jdom2

public Class Attribute

extends CloneBase
implements NamespaceAware, Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable, org.jdom2.NamespaceAware
Imports
java.io.Serializable, java.util.ArrayList, .Collections, .List, .TreeMap

An XML attribute. Methods allow the user to obtain the value of the attribute as well as namespace and type information.

JDOM 1.x Compatibility Note:
The Attribute class in JDOM 1.x had a number of int Constants declared to represent different Attribute Types. JDOM2 has introduced an AttributeType enumeration instead. To facilitate compatibility and to simplify JDOM 1.x migrations, the replacement AttributeType enums are referenced still using the JDOM 1.x constant names. In JDOM 1.x these names referenced constant int values. In JDOM2 these names reference Enum constants.

Authors
Brett McLaughlin, Jason Hunter, Elliotte Rusty Harold, Wesley Biggs, Victor Toni, Rolf Lear

Field Summary

Modifier and TypeField and Description
public static final AttributeType
CDATA_TYPE

JDOM 1.x compatible reference to AttributeType#CDATA

public static final AttributeType
ENTITIES_TYPE

JDOM 1.x compatible reference to AttributeType#ENTITIES

public static final AttributeType
ENTITY_TYPE

JDOM 1.x compatible reference to AttributeType#ENTITY

public static final AttributeType
ENUMERATED_TYPE

JDOM 1.x compatible reference to AttributeType#ENUMERATION

public static final AttributeType
ID_TYPE

JDOM 1.x compatible reference to AttributeType#ID

public static final AttributeType
IDREF_TYPE

JDOM 1.x compatible reference to AttributeType#IDREF

public static final AttributeType
IDREFS_TYPE

JDOM 1.x compatible reference to AttributeType#IDREFS

protected String
name

The local name of the Attribute

protected Namespace
namespace

The Namespace of the Attribute

public static final AttributeType
NMTOKEN_TYPE

JDOM 1.x compatible reference to AttributeType#NMTOKEN

public static final AttributeType
NMTOKENS_TYPE

JDOM 1.x compatible reference to AttributeType#NMTOKENS

public static final AttributeType
NOTATION_TYPE

JDOM 1.x compatible reference to AttributeType#NOTATION

protected transient Element
parent

The parent to which this Attribute belongs.

private static final long
serialVersionUID

JDOM 2.0.0 Serialization version.

protected boolean
specified

Specified attributes are part of the XML, unspecified attributes are 'defaulted' from a DTD.

protected AttributeType
type

The type of the Attribute

public static final AttributeType
UNDECLARED_TYPE

JDOM 1.x compatible reference to AttributeType#UNDECLARED

protected String
value

The value of the Attribute

Constructor Summary

AccessConstructor and Description
protected
Attribute()

Default, no-args constructor for implementations to use if needed.

public
Attribute(final String
String name of Attribute.
name
,
final String
String value for new attribute.
value
,
final Namespace
Namespace namespace for new attribute.
namespace
)

This will create a new Attribute with the specified (local) name and value, and in the provided Namespace.

public
Attribute(final String
String name of Attribute.
name
,
final String
String value for new attribute.
value
,
final int
int type for new attribute.
type
,
final Namespace
Namespace namespace for new attribute.
namespace
)

This will create a new Attribute with the specified (local) name, value, and type, and in the provided Namespace.
public
Attribute(final String
String name of Attribute.
name
,
final String
String value for new attribute.
value
,
final AttributeType
AttributeType for new attribute.
type
,
final Namespace
Namespace namespace for new attribute.
namespace
)

This will create a new Attribute with the specified (local) name, value, and type, and in the provided Namespace.

public
Attribute(final String
String name of Attribute.
name
,
final String
String value for new attribute.
value
)

This will create a new Attribute with the specified (local) name and value, and does not place the attribute in a Namespace.

public
Attribute(final String
String name of Attribute.
name
,
final String
String value for new attribute.
value
,
final AttributeType
AttributeType for new attribute.
type
)

This will create a new Attribute with the specified (local) name, value and type, and does not place the attribute in a Namespace.

public
Attribute(final String
String name of Attribute.
name
,
final String
String value for new attribute.
value
,
final int
int type for new attribute.
type
)

This will create a new Attribute with the specified (local) name, value and type, and does not place the attribute in a Namespace.

Method Summary

Modifier and TypeMethod and Description
public Attribute
clone()

Overrides org.jdom2.CloneBase.clone.

Return a deep clone of this instance.
public Attribute

Returns:

this Attribute (detached).
detach
()

Detach this Attribute from its parent.

public AttributeType

Returns:

AttributeType - type for this attribute.
getAttributeType
()

This will return the declared type of this Attribute.

public boolean

Returns:

boolean value of attribute.
getBooleanValue
()

This gets the effective boolean value of the attribute, or throws a DataConversionException if a conversion can't be performed.

public Document

Returns:

The document to which this Attribute is associated, may be null.
getDocument
()

Get this Attribute's Document.

public double

Returns:

double value of attribute.
getDoubleValue
()

This gets the value of the attribute, in double form, and if no conversion can occur, throws a DataConversionException

public float

Returns:

float value of attribute.
getFloatValue
()

This gets the value of the attribute, in float form, and if no conversion can occur, throws a DataConversionException

public int

Returns:

int value of attribute.
getIntValue
()

This gets the value of the attribute, in int form, and if no conversion can occur, throws a DataConversionException

public long

Returns:

long value of attribute.
getLongValue
()

This gets the value of the attribute, in long form, and if no conversion can occur, throws a DataConversionException

public String

Returns:

String - name of this attribute, without any namespace prefix.
getName
()

This will retrieve the local name of the Attribute.

public Namespace

Returns:

Namespace - Namespace object for this Attribute
getNamespace
()

This will return this Attribute's Namespace.

public String

Returns:

String - namespace prefix of this attribute.
getNamespacePrefix
()

This will retrieve the namespace prefix of the Attribute.

public List<Namespace>
getNamespacesInherited()

Implements org.jdom2.NamespaceAware.getNamespacesInherited.

Obtain a list of all namespaces that are in scope for this content, but were not introduced by this content.
public List<Namespace>
getNamespacesInScope()

Implements org.jdom2.NamespaceAware.getNamespacesInScope.

Get the namespaces that are in-scope on this Attribute.
public List<Namespace>
getNamespacesIntroduced()

Implements org.jdom2.NamespaceAware.getNamespacesIntroduced.

Obtain a list of all namespaces that are introduced to the XML tree by this node.
public String

Returns:

String - namespace URI for this Attribute.
getNamespaceURI
()

This returns the URI mapped to this Attribute's prefix.

public Element

Returns:

parent of this Attribute
getParent
()

This will return the parent of this Attribute.

public String

Returns:

String - full name for this element.
getQualifiedName
()

This will retrieve the qualified name of the Attribute.

public String

Returns:

String - value for this attribute.
getValue
()

This will return the actual textual value of this Attribute.

public boolean

Returns:

the specified flag.
isSpecified
()

Get the 'specified' flag.

private static final List<Namespace>
orderFirst(final Namespace nsa, final List<Namespace> nsl)

public Attribute

Returns:

Attribute - this Attribute modified.
setAttributeType
(final AttributeType
int type for the attribute.
type
)

This will set the type of the Attribute.

public Attribute

Returns:

Attribute - this Attribute modified.
setAttributeType
(final int
int type for the attribute.
type
)

This will set the type of the Attribute.
public Attribute

Returns:

Attribute - the attribute modified.
setName
(final String
the new local name to set
name
)

This sets the local name of the Attribute.

public Attribute

Returns:

Element - the element modified.
setNamespace
(Namespace
the new namespace
namespace
)

This sets this Attribute's Namespace.

protected Attribute

Returns:

this Attribute (state may be indeterminate depending on whether this has been included in the Element's list yet).
setParent
(Element
The parent to set
parent
)

Set this Attribute's parent.

public void
setSpecified(boolean
The value to set the specified flag to.
specified
)

Change the specified flag to the given value.

public Attribute

Returns:

Attribute - this Attribute modified.
setValue
(final String
String value for the attribute.
value
)

This will set the value of the Attribute.

public String

Returns:

String - information about the Attribute
toString
()

Overrides java.lang.Object.toString.

This returns a String representation of the Attribute, suitable for debugging.

Field Detail

CDATA_TYPEback to summary
public static final AttributeType CDATA_TYPE

JDOM 1.x compatible reference to AttributeType#CDATA

ENTITIES_TYPEback to summary
public static final AttributeType ENTITIES_TYPE

JDOM 1.x compatible reference to AttributeType#ENTITIES

ENTITY_TYPEback to summary
public static final AttributeType ENTITY_TYPE

JDOM 1.x compatible reference to AttributeType#ENTITY

ENUMERATED_TYPEback to summary
public static final AttributeType ENUMERATED_TYPE

JDOM 1.x compatible reference to AttributeType#ENUMERATION

ID_TYPEback to summary
public static final AttributeType ID_TYPE

JDOM 1.x compatible reference to AttributeType#ID

IDREF_TYPEback to summary
public static final AttributeType IDREF_TYPE

JDOM 1.x compatible reference to AttributeType#IDREF

IDREFS_TYPEback to summary
public static final AttributeType IDREFS_TYPE

JDOM 1.x compatible reference to AttributeType#IDREFS

nameback to summary
protected String name

The local name of the Attribute

namespaceback to summary
protected Namespace namespace

The Namespace of the Attribute

NMTOKEN_TYPEback to summary
public static final AttributeType NMTOKEN_TYPE

JDOM 1.x compatible reference to AttributeType#NMTOKEN

NMTOKENS_TYPEback to summary
public static final AttributeType NMTOKENS_TYPE

JDOM 1.x compatible reference to AttributeType#NMTOKENS

NOTATION_TYPEback to summary
public static final AttributeType NOTATION_TYPE

JDOM 1.x compatible reference to AttributeType#NOTATION

parentback to summary
protected transient Element parent

The parent to which this Attribute belongs. Change it with setParent(Element)

serialVersionUIDback to summary
private static final long serialVersionUID

JDOM 2.0.0 Serialization version. Attribute is simple

specifiedback to summary
protected boolean specified

Specified attributes are part of the XML, unspecified attributes are 'defaulted' from a DTD.

typeback to summary
protected AttributeType type

The type of the Attribute

UNDECLARED_TYPEback to summary
public static final AttributeType UNDECLARED_TYPE

JDOM 1.x compatible reference to AttributeType#UNDECLARED

valueback to summary
protected String value

The value of the Attribute

Constructor Detail

Attributeback to summary
protected Attribute()

Default, no-args constructor for implementations to use if needed.

Attributeback to summary
public Attribute(final String name, final String value, final Namespace namespace)

This will create a new Attribute with the specified (local) name and value, and in the provided Namespace.

Parameters
name:String

String name of Attribute.

value:String

String value for new attribute.

namespace:Namespace

Namespace namespace for new attribute.

Exceptions
IllegalNameException:
if the given name is illegal as an attribute name or if if the new namespace is the default namespace. Attributes cannot be in a default namespace.
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData).
Attributeback to summary
public Attribute(final String name, final String value, final int type, final Namespace namespace)

This will create a new Attribute with the specified (local) name, value, and type, and in the provided Namespace.

Parameters
name:String

String name of Attribute.

value:String

String value for new attribute.

type:int

int type for new attribute.

namespace:Namespace

Namespace namespace for new attribute.

Annotations
@Deprecated
Exceptions
IllegalNameException:
if the given name is illegal as an attribute name or if if the new namespace is the default namespace. Attributes cannot be in a default namespace.
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData) or if the given attribute type is not one of the supported types.
Attributeback to summary
public Attribute(final String name, final String value, final AttributeType type, final Namespace namespace)

This will create a new Attribute with the specified (local) name, value, and type, and in the provided Namespace.

Parameters
name:String

String name of Attribute.

value:String

String value for new attribute.

type:AttributeType

AttributeType for new attribute.

namespace:Namespace

Namespace namespace for new attribute.

Exceptions
IllegalNameException:
if the given name is illegal as an attribute name or if if the new namespace is the default namespace. Attributes cannot be in a default namespace.
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData) or if the given attribute type is not one of the supported types.
Attributeback to summary
public Attribute(final String name, final String value)

This will create a new Attribute with the specified (local) name and value, and does not place the attribute in a Namespace.

Note

This actually explicitly puts the Attribute in the "empty" Namespace (Namespace#NO_NAMESPACE).

Parameters
name:String

String name of Attribute.

value:String

String value for new attribute.

Exceptions
IllegalNameException:
if the given name is illegal as an attribute name.
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData).
Attributeback to summary
public Attribute(final String name, final String value, final AttributeType type)

This will create a new Attribute with the specified (local) name, value and type, and does not place the attribute in a Namespace.

Note

This actually explicitly puts the Attribute in the "empty" Namespace (Namespace#NO_NAMESPACE).

Parameters
name:String

String name of Attribute.

value:String

String value for new attribute.

type:AttributeType

AttributeType for new attribute.

Exceptions
IllegalNameException:
if the given name is illegal as an attribute name.
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData) or if the given attribute type is not one of the supported types.
Attributeback to summary
public Attribute(final String name, final String value, final int type)

This will create a new Attribute with the specified (local) name, value and type, and does not place the attribute in a Namespace.

Note

This actually explicitly puts the Attribute in the "empty" Namespace (Namespace#NO_NAMESPACE).

Parameters
name:String

String name of Attribute.

value:String

String value for new attribute.

type:int

int type for new attribute.

Annotations
@Deprecated
Exceptions
IllegalNameException:
if the given name is illegal as an attribute name.
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData) or if the given attribute type is not one of the supported types.

Method Detail

cloneback to summary
public Attribute clone()

Overrides org.jdom2.CloneBase.clone.

Doc from org.jdom2.CloneBase.clone.

Return a deep clone of this instance. Even if this instance has a parent, the returned clone will not.

All JDOM core classes are Cloneable, and never throw CloneNotSupportedException. Additionally all Cloneable JDOM classes return the correct type of instance from this method and there is no need to cast the result (co-variant return value).

Subclasses of this should still call super.clone() in their clone method.

Returns:Attribute

Doc from java.lang.Object.clone.

a clone of this instance.

Annotations
@Override
detachback to summary
public Attribute detach()

Detach this Attribute from its parent.

Returns:Attribute

this Attribute (detached).

getAttributeTypeback to summary
public AttributeType getAttributeType()

This will return the declared type of this Attribute.

Returns:AttributeType

AttributeType - type for this attribute.

getBooleanValueback to summary
public boolean getBooleanValue() throws DataConversionException

This gets the effective boolean value of the attribute, or throws a DataConversionException if a conversion can't be performed. True values are: "true", "on", "1", and "yes". False values are: "false", "off", "0", and "no". Values are trimmed before comparison. Values other than those listed here throw the exception.

Returns:boolean

boolean value of attribute.

Exceptions
DataConversionException:
when conversion fails.
getDocumentback to summary
public Document getDocument()

Get this Attribute's Document.

Returns:Document

The document to which this Attribute is associated, may be null.

getDoubleValueback to summary
public double getDoubleValue() throws DataConversionException

This gets the value of the attribute, in double form, and if no conversion can occur, throws a DataConversionException

Returns:double

double value of attribute.

Exceptions
DataConversionException:
when conversion fails.
getFloatValueback to summary
public float getFloatValue() throws DataConversionException

This gets the value of the attribute, in float form, and if no conversion can occur, throws a DataConversionException

Returns:float

float value of attribute.

Exceptions
DataConversionException:
when conversion fails.
getIntValueback to summary
public int getIntValue() throws DataConversionException

This gets the value of the attribute, in int form, and if no conversion can occur, throws a DataConversionException

Returns:int

int value of attribute.

Exceptions
DataConversionException:
when conversion fails.
getLongValueback to summary
public long getLongValue() throws DataConversionException

This gets the value of the attribute, in long form, and if no conversion can occur, throws a DataConversionException

Returns:long

long value of attribute.

Exceptions
DataConversionException:
when conversion fails.
getNameback to summary
public String getName()

This will retrieve the local name of the Attribute. For any XML attribute which appears as [namespacePrefix]:[attributeName], the local name of the attribute would be [attributeName]. When the attribute has no namespace, the local name is simply the attribute name.

To obtain the namespace prefix for this attribute, the getNamespacePrefix() method should be used.

Returns:String

String - name of this attribute, without any namespace prefix.

getNamespaceback to summary
public Namespace getNamespace()

This will return this Attribute's Namespace.

Returns:Namespace

Namespace - Namespace object for this Attribute

getNamespacePrefixback to summary
public String getNamespacePrefix()

This will retrieve the namespace prefix of the Attribute. For any XML attribute which appears as [namespacePrefix]:[attributeName], the namespace prefix of the attribute would be [namespacePrefix]. When the attribute has no namespace, an empty String is returned.

Returns:String

String - namespace prefix of this attribute.

getNamespacesInheritedback to summary
public List<Namespace> getNamespacesInherited()

Implements org.jdom2.NamespaceAware.getNamespacesInherited.

Doc from org.jdom2.NamespaceAware.getNamespacesInherited.

Obtain a list of all namespaces that are in scope for this content, but were not introduced by this content.

The contents of this list will always be a subset (but in the same order) of getNamespacesInScope(), and will never intersect getNamspacesIntroduced()

Returns:List<Namespace>

a read-only list of Namespaces.

Annotations
@Override
getNamespacesInScopeback to summary
public List<Namespace> getNamespacesInScope()

Implements org.jdom2.NamespaceAware.getNamespacesInScope.

Doc from org.jdom2.NamespaceAware.getNamespacesInScope.

Get the namespaces that are in-scope on this Attribute.

Attribute has peculiarities that affect the in-scope Namespaces because there are conditions in which the Attribute's scope is different to its parent Element's scope. Specifically, if the parent Element is in a 'default' Namespace that is not the empty Namespace (e.g. xmlns="someurl") and this Attribute is also in the default Namespace (has no prefix - but for Attributes that means the Namespace URL is ""), then this Attribute has a different namespace scope from its parent Element because it does not include the 'someurl' Namespace.

In the above conditions (no-prefix Attribute in an Element with a non-empty no-prefix Namespace) this Attribute effectively re-binds the "" prefix to the "" URL, thus the Attribute 'introduces' the Namespace. It follows then that the getNamespacesIntroduced() will return a list with the single member Namespace#NO_NAMESPACE.

Note that the Attribute's Namespace will always be reported first.

Description copied from NamespaceAware#getNamespacesInScope():

Obtain a list of all namespaces that are in scope for the current content.

The contents of this list will always be the combination of getNamespacesIntroduced() and getNamespacesInherited().

See NamespaceAware documentation for details on what the order of the Namespaces will be in the returned list.

Returns:List<Namespace>

a read-only list of Namespaces.

Annotations
@Override
getNamespacesIntroducedback to summary
public List<Namespace> getNamespacesIntroduced()

Implements org.jdom2.NamespaceAware.getNamespacesIntroduced.

Doc from org.jdom2.NamespaceAware.getNamespacesIntroduced.

Obtain a list of all namespaces that are introduced to the XML tree by this node. Only Elements and Attributes can introduce namespaces, so all other Content types will return an empty list.

The contents of this list will always be a subset (but in the same order) of getNamespacesInScope(), and will never intersect getNamspacesInherited()

Returns:List<Namespace>

a read-only list of Namespaces.

Annotations
@Override
getNamespaceURIback to summary
public String getNamespaceURI()

This returns the URI mapped to this Attribute's prefix. If no mapping is found, an empty String is returned.

Returns:String

String - namespace URI for this Attribute.

getParentback to summary
public Element getParent()

This will return the parent of this Attribute. If there is no parent, then this returns null. Use return-type covariance to override Content's getParent() method to return an Element, not just a Parent

Returns:Element

parent of this Attribute

getQualifiedNameback to summary
public String getQualifiedName()

This will retrieve the qualified name of the Attribute. For any XML attribute whose name is [namespacePrefix]:[elementName], the qualified name of the attribute would be everything (both namespace prefix and element name). When the attribute has no namespace, the qualified name is simply the attribute's local name.

To obtain the local name of the attribute, the getName() method should be used.

To obtain the namespace prefix for this attribute, the getNamespacePrefix() method should be used.

Returns:String

String - full name for this element.

getValueback to summary
public String getValue()

This will return the actual textual value of this Attribute. This will include all text within the quotation marks.

Returns:String

String - value for this attribute.

isSpecifiedback to summary
public boolean isSpecified()

Get the 'specified' flag. True values indicate this attribute was part of an XML document, false indicates it was defaulted from a DTD.

Returns:boolean

the specified flag.

Since
JDOM2
orderFirstback to summary
private static final List<Namespace> orderFirst(final Namespace nsa, final List<Namespace> nsl)
setAttributeTypeback to summary
public Attribute setAttributeType(final AttributeType type)

This will set the type of the Attribute.

Parameters
type:AttributeType

int type for the attribute.

Returns:Attribute

Attribute - this Attribute modified.

Exceptions
IllegalDataException:
if the given attribute type is not one of the supported types.
setAttributeTypeback to summary
public Attribute setAttributeType(final int type)

This will set the type of the Attribute.

Parameters
type:int

int type for the attribute.

Returns:Attribute

Attribute - this Attribute modified.

Annotations
@Deprecated
Exceptions
IllegalDataException:
if the given attribute type is not one of the supported types.
setNameback to summary
public Attribute setName(final String name)

This sets the local name of the Attribute.

Parameters
name:String

the new local name to set

Returns:Attribute

Attribute - the attribute modified.

Exceptions
IllegalNameException:
if the given name is illegal as an attribute name.
setNamespaceback to summary
public Attribute setNamespace(Namespace namespace)

This sets this Attribute's Namespace. If the provided namespace is null, the attribute will have no namespace. The namespace must have a prefix.

Parameters
namespace:Namespace

the new namespace

Returns:Attribute

Element - the element modified.

Exceptions
IllegalNameException:
if the new namespace is the default namespace. Attributes cannot be in a default namespace.
setParentback to summary
protected Attribute setParent(Element parent)

Set this Attribute's parent. This is not public!

Parameters
parent:Element

The parent to set

Returns:Attribute

this Attribute (state may be indeterminate depending on whether this has been included in the Element's list yet).

setSpecifiedback to summary
public void setSpecified(boolean specified)

Change the specified flag to the given value.

Parameters
specified:boolean

The value to set the specified flag to.

Since
JDOM2
setValueback to summary
public Attribute setValue(final String value)

This will set the value of the Attribute.

Parameters
value:String

String value for the attribute.

Returns:Attribute

Attribute - this Attribute modified.

Exceptions
IllegalDataException:
if the given attribute value is illegal character data (as determined by org.jdom2.Verifier#checkCharacterData).
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

This returns a String representation of the Attribute, suitable for debugging.

Returns:String

String - information about the Attribute

Annotations
@Override