Top Description Fields Constructors Methods
org.jdom2

public Class Element

extends Content
implements Parent
Class Inheritance
All Implemented Interfaces
org.jdom2.Parent, java.io.Serializable, org.jdom2.NamespaceAware, java.lang.Cloneable
Known Direct Subclasses
org.jdom2.located.LocatedElement
Static Imports
org.jdom2.JDOMConstants.NS_PREFIX_DEFAULT, .JDOMConstants.NS_PREFIX_XML

An XML element. Methods allow the user to get and manipulate its child elements and content, directly access the element's textual content, manipulate its attributes, and manage namespaces.

See NamespaceAware and getNamespacesInScope() for more details on what the Namespace scope is and how it is managed in JDOM and specifically by this Element class.

Authors
Brett McLaughlin, Jason Hunter, Lucas Gonze, Kevin Regan, Dan Schaffer, Yusuf Goolamabbas, Kent C. Johnson, Jools Enticknap, Alex Rosen, Bradley S. Huffman, Victor Toni, Rolf Lear
See Also
NamespaceAware, Content

Field Summary

Modifier and TypeField and Description
pack-priv transient List<Namespace>
additionalNamespaces

Additional namespace declarations to store on this element; useful during output

pack-priv transient AttributeList
attributes

The attributes of the element.

pack-priv transient ContentList
content

The content of the element.

private static final int
protected String
name

The local name of the element

protected Namespace
namespace

The namespace of the element

private static final long
Inherited from org.jdom2.Content:
ctypeparent

Constructor Summary

AccessConstructor and Description
protected
Element()

This protected constructor is provided in order to support an Element subclass that wants full control over variable initialization.

public
Element(final String
local name of the element
name
,
final Namespace
namespace for the element
namespace
)

Creates a new element with the supplied (local) name and namespace.

public
Element(final String
local name of the element
name
)

Create a new element with the supplied (local) name and no namespace.

public
Element(final String
name of the element
name
,
final String
namespace URI for the element
uri
)

Creates a new element with the supplied (local) name and a namespace given by a URI.

public
Element(final String
local name of the element
name
,
final String
namespace prefix
prefix
,
final String
namespace URI for the element
uri
)

Creates a new element with the supplied (local) name and a namespace given by the supplied prefix and URI combination.

Method Summary

Modifier and TypeMethod and Description
public Element

Returns:

this element modified
addContent
(final String
String to add
str
)

This adds text content to this element.

public Element

Returns:

the element on which the method was called
addContent
(final Content
child to append to end of content list
child
)

Implements org.jdom2.Parent.addContent.

Appends the child to the end of the element's content list.
public Element

Returns:

the element on which the method was called
addContent
(final Collection<? extends Content>
Collection of content to append
newContent
)

Implements org.jdom2.Parent.addContent.

Appends all children in the given collection to the end of the content list.
public Element

Returns:

the parent on which the method was called
addContent
(final int
location for adding the collection
index
,
final Content
child to insert
child
)

Implements org.jdom2.Parent.addContent.

Inserts the child into the content list at the given index.
public Element

Returns:

the parent on which the method was called
addContent
(final int
location for adding the collection
index
,
final Collection<? extends Content>
Collection of content to insert
newContent
)

Implements org.jdom2.Parent.addContent.

Inserts the content in a collection into the content list at the given index.
public boolean

Returns:

true if the namespace is added (false if it was previously added)
addNamespaceDeclaration
(final Namespace
namespace to add
additionalNamespace
)

Adds a namespace declarations to this element.

public void
canContainContent(Content
The content to be checked
child
,
int
The location where the content would be put.
index
,
boolean
true if the intention is to replace the content already at the index.
replace
)

Implements org.jdom2.Parent.canContainContent.

Test whether this Parent instance can contain the specified content at the specified position.
public Element

Returns:

the clone of this element
clone
()

Overrides org.jdom2.Content.clone.

Implements org.jdom2.Parent.clone.

This returns a deep clone of this element.

public List<Content>
cloneContent()

Implements org.jdom2.Parent.cloneContent.

Returns a list containing detached clones of this parent's content list.
public boolean

Returns:

true if any content was changed by this operation.
coalesceText
(boolean
true if you want the text of child elements coalesced too. False if you only want to coalesce this Element's Text.
recursively
)

Adjacent Text content is merged into the first Text in document order, and the redundant Text items are removed (including any empty Text).

public Element
detach()

Overrides org.jdom2.Content.detach.

Detaches this child from its parent or does nothing if the child has no parent.
public List<Namespace>

Returns:

a List of the additional namespace declarations
getAdditionalNamespaces
()

Returns a list of the additional namespace declarations on this element.

public Attribute

Returns:

attribute for the element
getAttribute
(final String
name of the attribute to return
attname
)

This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.

public Attribute

Returns:

attribute for the element
getAttribute
(final String
name of the attribute to return
attname
,
final Namespace
Namespace to search within. A null implies Namespace.NO_NAMESPACE.
ns
)

This returns the attribute for this element with the given name and within the given Namespace, or null if no such attribute exists.

pack-priv AttributeList

Returns:

this Element's Attribute List (creating it if necessary).
getAttributeList
()

Lazy initialiser for the Attribute list.

public List<Attribute>

Returns:

attributes for the element
getAttributes
()

This returns the complete set of attributes for this element, as a List of Attribute objects in no particular order, or an empty list if there are none.

public int

Returns:

the number of Attributes attached.
getAttributesSize
()

Get the number of Attributes currently attached to this Element.

public String

Returns:

the named attribute's value, or null if no such attribute
getAttributeValue
(final String
name of the attribute whose value to be returned
attname
)

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

public String

Returns:

the named attribute's value, or the default if no such attribute
getAttributeValue
(final String
name of the attribute whose value to be returned
attname
,
final String
a default value to return if the attribute does not exist
def
)

This returns the attribute value for the attribute with the given name and within no namespace, or the passed-in default if there is no such attribute.

public String

Returns:

the named attribute's value, or null if no such attribute
getAttributeValue
(final String
name of the attribute whose valud is to be returned
attname
,
final Namespace
Namespace to search within. A null implies Namespace.NO_NAMESPACE.
ns
)

This returns the attribute value for the attribute with the given name and within the given Namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

public String

Returns:

the named attribute's value, or the default if no such attribute
getAttributeValue
(final String
name of the attribute whose valud is to be returned
attname
,
final Namespace
Namespace to search within. A null implies Namespace.NO_NAMESPACE.
ns
,
final String
a default value to return if the attribute does not exist
def
)

This returns the attribute value for the attribute with the given name and within the given Namespace, or the passed-in default if there is no such attribute.

public Element

Returns:

the first matching child element, or null if not found
getChild
(final String
local name of child element to match. A null implies any name
cname
,
final Namespace
Namespace to search within. A null implies any namespace.
ns
)

This returns the first child element within this element with the given local name and belonging to the given namespace.

public Element

Returns:

the first matching child element, or null if not found
getChild
(final String
local name of child element to match
cname
)

This returns the first child element within this element with the given local name and belonging to no namespace.

public List<Element>

Returns:

list of child Element objects for this element
getChildren
()

This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.

public List<Element>

Returns:

all matching child elements
getChildren
(final String
local name for the children to match
cname
)

This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned as Element objects.

public List<Element>

Returns:

all matching child elements
getChildren
(final String
local name for the children to match
cname
,
final Namespace
Namespace to search within. A null implies Namespace.NO_NAMESPACE.
ns
)

This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to the given Namespace, returned as Element objects.

public String

Returns:

text content for the named child, or null if no such child
getChildText
(final String
the name of the child
cname
)

Returns the textual content of the named child element, or null if there's no such child.

public String

Returns:

text content for the named child, or null if no such child
getChildText
(final String
the name of the child
cname
,
final Namespace
the namespace of the child. A null implies Namespace.NO_NAMESPACE.
ns
)

Returns the textual content of the named child element, or null if there's no such child.

public String

Returns:

normalized text content for the named child, or null if no such child
getChildTextNormalize
(final String
the name of the child
cname
)

Returns the normalized textual content of the named child element, or null if there's no such child.

public String

Returns:

normalized text content for the named child, or null if no such child
getChildTextNormalize
(final String
the name of the child
cname
,
final Namespace
the namespace of the child. A null implies Namespace.NO_NAMESPACE.
ns
)

Returns the normalized textual content of the named child element, or null if there's no such child.

public String

Returns:

trimmed text content for the named child, or null if no such child
getChildTextTrim
(final String
the name of the child
cname
)

Returns the trimmed textual content of the named child element, or null if there's no such child.

public String

Returns:

trimmed text content for the named child, or null if no such child
getChildTextTrim
(final String
the name of the child
cname
,
final Namespace
the namespace of the child. A null implies Namespace.NO_NAMESPACE.
ns
)

Returns the trimmed textual content of the named child element, or null if there's no such child.

public List<Content>

Returns:

a List containing the mixed content of the element: may contain Text, Element, Comment, ProcessingInstruction, CDATA, and EntityRef objects.
getContent
()

Implements org.jdom2.Parent.getContent.

This returns the full content of the element as a List which may contain objects of type Text, Element, Comment, ProcessingInstruction, CDATA, and EntityRef.
public <E extends Content> List<E>

Returns:

List - filtered Element content
getContent
(final Filter<E>
Filter to apply Note that the Filters class has a number of predefined, useful filters.
filter
)

Implements org.jdom2.Parent.getContent.

Return a filter view of this Element's content.
public Content
getContent(final int
location of desired child
index
)

Implements org.jdom2.Parent.getContent.

Returns the child at the given index.
public int
getContentSize()

Implements org.jdom2.Parent.getContentSize.

Returns the number of children in this parent's content list.
public IteratorIterable<Content>

Returns:

an iterator to walk descendants
getDescendants
()

Implements org.jdom2.Parent.getDescendants.

Returns an iterator that walks over all descendants in document order.
public <F extends Content> IteratorIterable<F>

Returns:

an iterator to walk descendants within a filter
getDescendants
(final Filter<F>
filter to select which descendants to see Note that the Filters class has a number of predefined, useful filters.
filter
)

Implements org.jdom2.Parent.getDescendants.

Returns an iterator that walks over all descendants in document order applying the Filter to return only content that match the filter rule.
public String

Returns:

local element name
getName
()

Returns the (local) name of the element (without any namespace prefix).

public Namespace

Returns:

the element's namespace
getNamespace
()

Returns the element's Namespace.

public Namespace

Returns:

the Namespace for this prefix at this location, or null if none
getNamespace
(final String
namespace prefix to look up
prefix
)

Returns the Namespace corresponding to the given prefix in scope for this element.

public String

Returns:

the namespace prefix
getNamespacePrefix
()

Returns the namespace prefix of the element or an empty string if none exists.

public List<Namespace>
getNamespacesInherited()

Overrides org.jdom2.Content.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()

Overrides org.jdom2.Content.getNamespacesInScope.

Implements org.jdom2.NamespaceAware.getNamespacesInScope.

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

Overrides org.jdom2.Content.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:

the namespace URI for this element
getNamespaceURI
()

Returns the namespace URI mapped to this element's prefix (or the in-scope default namespace URI if no prefix).

public String

Returns:

qualified name of the element (including namespace prefix)
getQualifiedName
()

Returns the full name of the element, in the form [namespacePrefix]:[localName].

public String

Returns:

text content for this element, or empty string if none
getText
()

Returns the textual content directly held under this element as a string.

public String

Returns:

normalized text content for this element, or empty string if none
getTextNormalize
()

Returns the textual content of this element with all surrounding whitespace removed and internal whitespace normalized to a single space.

public String

Returns:

trimmed text content for this element, or empty string if none
getTextTrim
()

Returns the textual content of this element with all surrounding whitespace removed.

public String

Returns:

a concatenation of all text node descendants
getValue
()

Implements abstract org.jdom2.Content.getValue.

Returns the XPath 1.0 string value of this element, which is the complete, ordered content of all text node descendants of this element (i.e. the text that's left after all references are resolved and all other markup is stripped out.)
public URI

Returns:

a URI representing the XMLBase value for the supplied Element, or null if one could not be calculated.
getXMLBaseURI
()

Calculate the XMLBase URI for this Element using the rules defined in the XMLBase specification, as well as the values supplied in the xml:base attributes on this Element and its ancestry.

public boolean

Returns:

true if this Element has additional Namespaces.
hasAdditionalNamespaces
()

Indicate whether this Element has any additional Namespace declarations.

public boolean

Returns:

true if this Element has attributes.
hasAttributes
()

Indicate whether this Element has any attributes.

public int
indexOf(final Content
child to search for
child
)

Implements org.jdom2.Parent.indexOf.

Returns the index of the supplied child in the content list, or -1 if not a child of this parent.
public boolean

Returns:

true if this element is the ancestor of the supplied element
isAncestor
(final Element
Element to check against
element
)

Determines if this element is the ancestor of another element.

public boolean

Returns:

whether this is a root element
isRootElement
()

Returns whether this element is a root element.

private void
readObject(final ObjectInputStream
where to read the Element from.
in
)

Read an Element off the ObjectInputStream.

public boolean

Returns:

whether the attribute was removed
removeAttribute
(final String
name of attribute to remove
attname
)

This removes the attribute with the given name and within no namespace.

public boolean

Returns:

whether the attribute was removed
removeAttribute
(final String
name of attribute to remove
attname
,
final Namespace
namespace URI of attribute to remove. A null implies Namespace.NO_NAMESPACE.
ns
)

This removes the attribute with the given name and within the given Namespace.

public boolean

Returns:

whether the attribute was removed
removeAttribute
(final Attribute
Reference to the attribute to be removed.
attribute
)

This removes the supplied Attribute should it exist.

public boolean

Returns:

whether deletion occurred
removeChild
(final String
the name of child elements to remove
cname
)

This removes the first child element (one level deep) with the given local name and belonging to no namespace.

public boolean

Returns:

whether deletion occurred
removeChild
(final String
the name of child element to remove
cname
,
final Namespace
Namespace to search within. A null implies Namespace.NO_NAMESPACE.
ns
)

This removes the first child element (one level deep) with the given local name and belonging to the given namespace.

public boolean

Returns:

whether deletion occurred
removeChildren
(final String
the name of child elements to remove
cname
)

This removes all child elements (one level deep) with the given local name and belonging to no namespace.

public boolean

Returns:

whether deletion occurred
removeChildren
(final String
the name of child elements to remove
cname
,
final Namespace
Namespace to search within. A null implies Namespace.NO_NAMESPACE.
ns
)

This removes all child elements (one level deep) with the given local name and belonging to the given namespace.

public List<Content>

Returns:

list of the old children detached from this parent
removeContent
()

Implements org.jdom2.Parent.removeContent.

Removes all child content from this parent.
public <F extends Content> List<F>

Returns:

list of the old children detached from this parent
removeContent
(final Filter<F>
filter to select which content to remove Note that the Filters class has a number of predefined, useful filters.
filter
)

Implements org.jdom2.Parent.removeContent.

Remove all child content from this parent matching the supplied filter.
public boolean
removeContent(final Content
child to remove
child
)

Implements org.jdom2.Parent.removeContent.

Removes a single child node from the content list.
public Content
removeContent(final int
index of child to remove
index
)

Implements org.jdom2.Parent.removeContent.

Removes and returns the child at the given index, or returns null if there's no such child.
public void
removeNamespaceDeclaration(final Namespace
namespace to remove. A null Namespace does nothing.
additionalNamespace
)

Removes an additional namespace declarations from this element.

private final URI

Returns:

the resulting URI, may be null.
resolve
(String
'currently' URI as a string
uri
,
URI
the relative URI we are trying to make absolute
relative
)

Simple method that supports getXMLBaseURI().

public Element

Returns:

this element modified
setAttribute
(final String
name of the attribute to set
name
,
final String
value of the attribute to set
value
)

This sets an attribute value for this element.

public Element

Returns:

this element modified
setAttribute
(final String
name of the attribute to set
name
,
final String
value of the attribute to set
value
,
final Namespace
namespace of the attribute to set. A null implies Namespace.NO_NAMESPACE.
ns
)

This sets an attribute value for this element.

public Element

Returns:

this element modified
setAttribute
(final Attribute
Attribute to set
attribute
)

This sets an attribute value for this element.

public Element

Returns:

this element modified
setAttributes
(final Collection<? extends Attribute>
Collection of attributes to set
newAttributes
)

This sets the attributes of the element.

public Element

Returns:

this element modified
setContent
(final Collection<? extends Content>
Collection of content to set
newContent
)

This sets the content of the element.

public Element

Returns:

element on which this method was invoked
setContent
(final int
- index of child to replace.
index
,
final Content
- child to add.
child
)

Replace the current child the given index with the supplied child.

public Parent

Returns:

object on which this method was invoked
setContent
(final int
- index of child to replace.
index
,
final Collection<? extends Content>
- Collection of content to replace child.
newContent
)

Replace the child at the given index with the supplied collection.

public Element

Returns:

the parent on which the method was called
setContent
(final Content
new content to replace existing content
child
)

Set this element's content to be the supplied child.

public Element

Returns:

the target element
setName
(final String
the new (local) name of the element
name
)

Sets the (local) name of the element.

public Element

Returns:

the target element
setNamespace
(Namespace
the new namespace. A null implies Namespace.NO_NAMESPACE.
namespace
)

Sets the element's Namespace.

public Element

Returns:

the target element
setText
(final String
new text content for the element
text
)

Sets the content of the element to be the text given.

public void
sortAttributes(Comparator<? super Attribute>
The Comparator to use for the sorting.
comparator
)

Sort the Attributes of this Element using a mechanism that is safe for JDOM.

public void
sortChildren(Comparator<? super Element>
The Comparator to use for the sorting.
comparator
)

Sort the child Elements of this Element using a mechanism that is safe for JDOM content.

public void
sortContent(Comparator<? super Content>
The Comparator to use for the sorting.
comparator
)

Sort the contents of this Element using a mechanism that is safe for JDOM content.

public <
The generic type of the Filter used to select the content to sort.
E extends Content
>
void
sortContent(Filter<E>
The Filter used to select which child content to sort. Note that the Filters class has a number of predefined, useful filters.
filter
,
Comparator<? super E>
The Comparator to use for the sorting.
comparator
)

Sort the child content of this Element that matches the Filter, using a mechanism that is safe for JDOM content.

public String

Returns:

String - information about the Element
toString
()

Overrides java.lang.Object.toString.

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

private void
writeObject(final ObjectOutputStream
where to write the Element to.
out
)

Serialize out the Element.

Inherited from org.jdom2.Content:
equalsgetCTypegetDocumentgetParentgetParentElementhashCodesetParent