public class Element extends Content implements Parent
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.
NamespaceAware
,
Content
,
Serialized FormContent.CType
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
name
The local name of the element
|
protected Namespace |
namespace
The namespace of the element
|
Modifier | Constructor and Description |
---|---|
protected |
Element()
This protected constructor is provided in order to support an Element
subclass that wants full control over variable initialization.
|
|
Element(java.lang.String name)
Create a new element with the supplied (local) name and no namespace.
|
|
Element(java.lang.String name,
Namespace namespace)
Creates a new element with the supplied (local) name and namespace.
|
|
Element(java.lang.String name,
java.lang.String uri)
Creates a new element with the supplied (local) name and a namespace
given by a URI.
|
|
Element(java.lang.String name,
java.lang.String prefix,
java.lang.String uri)
Creates a new element with the supplied (local) name and a namespace
given by the supplied prefix and URI combination.
|
Modifier and Type | Method and Description |
---|---|
Element |
addContent(java.util.Collection<? extends Content> newContent)
Appends all children in the given collection to the end of
the content list.
|
Element |
addContent(Content child)
Appends the child to the end of the element's content list.
|
Element |
addContent(int index,
java.util.Collection<? extends Content> newContent)
Inserts the content in a collection into the content list
at the given index.
|
Element |
addContent(int index,
Content child)
Inserts the child into the content list at the given index.
|
Element |
addContent(java.lang.String str)
This adds text content to this element.
|
boolean |
addNamespaceDeclaration(Namespace additionalNamespace)
Adds a namespace declarations to this element.
|
void |
canContainContent(Content child,
int index,
boolean replace)
Test whether this Parent instance can contain the specified content
at the specified position.
|
Element |
clone()
This returns a deep clone of this element.
|
java.util.List<Content> |
cloneContent()
Returns a list containing detached clones of this parent's content list.
|
boolean |
coalesceText(boolean recursively)
Adjacent Text content is merged into the first Text in document order,
and the redundant Text items are removed (including any empty Text).
|
Element |
detach()
Detaches this child from its parent or does nothing if the child
has no parent.
|
java.util.List<Namespace> |
getAdditionalNamespaces()
Returns a list of the additional namespace declarations on this element.
|
Attribute |
getAttribute(java.lang.String attname)
This returns the attribute for this element with the given name
and within no namespace, or null if no such attribute exists.
|
Attribute |
getAttribute(java.lang.String attname,
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.
|
java.util.List<Attribute> |
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. |
int |
getAttributesSize()
Get the number of Attributes currently attached to this Element.
|
java.lang.String |
getAttributeValue(java.lang.String 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.
|
java.lang.String |
getAttributeValue(java.lang.String attname,
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.
|
java.lang.String |
getAttributeValue(java.lang.String attname,
Namespace ns,
java.lang.String 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.
|
java.lang.String |
getAttributeValue(java.lang.String attname,
java.lang.String 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.
|
Element |
getChild(java.lang.String cname)
This returns the first child element within this element with the
given local name and belonging to no namespace.
|
Element |
getChild(java.lang.String cname,
Namespace ns)
This returns the first child element within this element with the
given local name and belonging to the given namespace.
|
java.util.List<Element> |
getChildren()
This returns a
List of all the child elements
nested directly (one level deep) within this element, as
Element objects. |
java.util.List<Element> |
getChildren(java.lang.String 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. |
java.util.List<Element> |
getChildren(java.lang.String cname,
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. |
java.lang.String |
getChildText(java.lang.String cname)
Returns the textual content of the named child element, or null if
there's no such child.
|
java.lang.String |
getChildText(java.lang.String cname,
Namespace ns)
Returns the textual content of the named child element, or null if
there's no such child.
|
java.lang.String |
getChildTextNormalize(java.lang.String cname)
Returns the normalized textual content of the named child element, or
null if there's no such child.
|
java.lang.String |
getChildTextNormalize(java.lang.String cname,
Namespace ns)
Returns the normalized textual content of the named child element, or
null if there's no such child.
|
java.lang.String |
getChildTextTrim(java.lang.String cname)
Returns the trimmed textual content of the named child element, or null
if there's no such child.
|
java.lang.String |
getChildTextTrim(java.lang.String cname,
Namespace ns)
Returns the trimmed textual content of the named child element, or null
if there's no such child.
|
java.util.List<Content> |
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 . |
<E extends Content> |
getContent(Filter<E> filter)
Return a filter view of this
Element 's content. |
Content |
getContent(int index)
Returns the child at the given index.
|
int |
getContentSize()
Returns the number of children in this parent's content list.
|
IteratorIterable<Content> |
getDescendants()
Returns an iterator that walks over all descendants in document order.
|
<F extends Content> |
getDescendants(Filter<F> filter)
Returns an iterator that walks over all descendants in document order
applying the Filter to return only content that match the filter rule.
|
java.lang.String |
getName()
Returns the (local) name of the element (without any namespace prefix).
|
Namespace |
getNamespace()
Returns the element's
Namespace . |
Namespace |
getNamespace(java.lang.String prefix)
Returns the
Namespace corresponding to the given prefix in scope
for this element. |
java.lang.String |
getNamespacePrefix()
Returns the namespace prefix of the element or an empty string if none
exists.
|
java.util.List<Namespace> |
getNamespacesInherited()
Obtain a list of all namespaces that are in scope for this content, but
were not introduced by this content.
|
java.util.List<Namespace> |
getNamespacesInScope()
Get the Namespaces that are in-scope on this Element.
|
java.util.List<Namespace> |
getNamespacesIntroduced()
Obtain a list of all namespaces that are introduced to the XML tree by
this node.
|
java.lang.String |
getNamespaceURI()
Returns the namespace URI mapped to this element's prefix (or the
in-scope default namespace URI if no prefix).
|
java.lang.String |
getQualifiedName()
Returns the full name of the element, in the form
[namespacePrefix]:[localName].
|
java.lang.String |
getText()
Returns the textual content directly held under this element as a string.
|
java.lang.String |
getTextNormalize()
Returns the textual content of this element with all surrounding
whitespace removed and internal whitespace normalized to a single space.
|
java.lang.String |
getTextTrim()
Returns the textual content of this element with all surrounding
whitespace removed.
|
java.lang.String |
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.)
|
java.net.URI |
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.
|
boolean |
hasAdditionalNamespaces()
Indicate whether this Element has any additional Namespace declarations.
|
boolean |
hasAttributes()
Indicate whether this Element has any attributes.
|
int |
indexOf(Content child)
Returns the index of the supplied child in the content list,
or -1 if not a child of this parent.
|
boolean |
isAncestor(Element element)
Determines if this element is the ancestor of another element.
|
boolean |
isRootElement()
Returns whether this element is a root element.
|
boolean |
removeAttribute(Attribute attribute)
This removes the supplied Attribute should it exist.
|
boolean |
removeAttribute(java.lang.String attname)
This removes the attribute with the given name and within no
namespace.
|
boolean |
removeAttribute(java.lang.String attname,
Namespace ns)
This removes the attribute with the given name and within the
given Namespace.
|
boolean |
removeChild(java.lang.String cname)
This removes the first child element (one level deep) with the
given local name and belonging to no namespace.
|
boolean |
removeChild(java.lang.String cname,
Namespace ns)
This removes the first child element (one level deep) with the
given local name and belonging to the given namespace.
|
boolean |
removeChildren(java.lang.String cname)
This removes all child elements (one level deep) with the
given local name and belonging to no namespace.
|
boolean |
removeChildren(java.lang.String cname,
Namespace ns)
This removes all child elements (one level deep) with the
given local name and belonging to the given namespace.
|
java.util.List<Content> |
removeContent()
Removes all child content from this parent.
|
boolean |
removeContent(Content child)
Removes a single child node from the content list.
|
<F extends Content> |
removeContent(Filter<F> filter)
Remove all child content from this parent matching the supplied filter.
|
Content |
removeContent(int index)
Removes and returns the child at the given
index, or returns null if there's no such child.
|
void |
removeNamespaceDeclaration(Namespace additionalNamespace)
Removes an additional namespace declarations from this element.
|
Element |
setAttribute(Attribute attribute)
This sets an attribute value for this element.
|
Element |
setAttribute(java.lang.String name,
java.lang.String value)
This sets an attribute value for this element.
|
Element |
setAttribute(java.lang.String name,
java.lang.String value,
Namespace ns)
This sets an attribute value for this element.
|
Element |
setAttributes(java.util.Collection<? extends Attribute> newAttributes)
This sets the attributes of the element.
|
Element |
setContent(java.util.Collection<? extends Content> newContent)
This sets the content of the element.
|
Element |
setContent(Content child)
Set this element's content to be the supplied child.
|
Parent |
setContent(int index,
java.util.Collection<? extends Content> newContent)
Replace the child at the given index with the supplied
collection.
|
Element |
setContent(int index,
Content child)
Replace the current child the given index with the supplied child.
|
Element |
setName(java.lang.String name)
Sets the (local) name of the element.
|
Element |
setNamespace(Namespace namespace)
Sets the element's
Namespace . |
Element |
setText(java.lang.String text)
Sets the content of the element to be the text given.
|
void |
sortAttributes(java.util.Comparator<? super Attribute> comparator)
Sort the Attributes of this Element using a mechanism that is safe
for JDOM.
|
void |
sortChildren(java.util.Comparator<? super Element> comparator)
Sort the child Elements of this Element using a mechanism that is safe
for JDOM content.
|
void |
sortContent(java.util.Comparator<? super Content> comparator)
Sort the contents of this Element using a mechanism that is safe for JDOM
content.
|
<E extends Content> |
sortContent(Filter<E> filter,
java.util.Comparator<? super E> comparator)
Sort the child content of this Element that matches the Filter, using a
mechanism that is safe for JDOM content.
|
java.lang.String |
toString()
This returns a
String representation of the
Element , suitable for debugging. |
equals, getCType, getDocument, getParent, getParentElement, hashCode, setParent
finalize, getClass, notify, notifyAll, wait, wait, wait
getDocument, getParent
protected java.lang.String name
protected Namespace namespace
protected Element()
When implementing an Element subclass which doesn't require full control over variable initialization, be aware that simply calling super() (or letting the compiler add the implicit super() call) will not initialize the instance variables which will cause many of the methods to throw a NullPointerException. Therefore, the constructor for these subclasses should call one of the public constructors so variable initialization is handled automatically.
public Element(java.lang.String name, Namespace namespace)
name
- local name of the elementnamespace
- namespace for the elementIllegalNameException
- if the given name is illegal as an element
namepublic Element(java.lang.String name)
name
- local name of the elementIllegalNameException
- if the given name is illegal as an element
name.public Element(java.lang.String name, java.lang.String uri)
name
- name of the elementuri
- namespace URI for the elementIllegalNameException
- if the given name is illegal as an element
name or the given URI is illegal as a
namespace URIpublic Element(java.lang.String name, java.lang.String prefix, java.lang.String uri)
name
- local name of the elementprefix
- namespace prefixuri
- namespace URI for the elementIllegalNameException
- if the given name is illegal as an element
name, the given prefix is illegal as a
namespace prefix, or the given URI is
illegal as a namespace URIpublic java.lang.String getName()
public Element setName(java.lang.String name)
name
- the new (local) name of the elementIllegalNameException
- if the given name is illegal as an Element
namepublic Namespace getNamespace()
Namespace
.public Element setNamespace(Namespace namespace)
Namespace
. If the provided namespace is null,
the element will have no namespace.namespace
- the new namespace. A null implies Namespace.NO_NAMESPACE.IllegalAddException
- if there is a Namespace conflictpublic java.lang.String getNamespacePrefix()
public java.lang.String getNamespaceURI()
public Namespace getNamespace(java.lang.String prefix)
Namespace
corresponding to the given prefix in scope
for this element. This involves searching up the tree, so the results
depend on the current location of the element. Returns null if there is
no namespace in scope with the given prefix at this point in the
document.prefix
- namespace prefix to look uppublic java.lang.String getQualifiedName()
public boolean addNamespaceDeclaration(Namespace additionalNamespace)
additionalNamespace
- namespace to addIllegalAddException
- if the namespace prefix collides with another
namespace prefix on the elementpublic void removeNamespaceDeclaration(Namespace additionalNamespace)
additionalNamespace
- namespace to remove. A null Namespace does nothing.public java.util.List<Namespace> getAdditionalNamespaces()
getNamespace()
. If there
are no additional declarations, this returns an empty list. Note, the
returned list is unmodifiable.public java.lang.String getValue()
public boolean isRootElement()
Content.getParent()
to determine if an element has any
"attachments" to a parent element or document.
An element is a root element when it has a parent and that parent is a Document. In particular, this means that detached Elements are not root elements.
public int getContentSize()
Parent
Content
type.getContentSize
in interface Parent
public int indexOf(Content child)
Parent
public java.lang.String getText()
Text
and CDATA
nodes returned by getContent()
.
The call does not recurse into child elements. If no textual value exists
for the element, an empty string is returned.public java.lang.String getTextTrim()
public java.lang.String getTextNormalize()
public java.lang.String getChildText(java.lang.String cname)
getChild().getText()
can throw a NullPointerException.cname
- the name of the childpublic java.lang.String getChildTextTrim(java.lang.String cname)
getTextTrim()
for
details of text trimming.cname
- the name of the childpublic java.lang.String getChildTextNormalize(java.lang.String cname)
getTextNormalize()
for details of text normalizing.cname
- the name of the childpublic java.lang.String getChildText(java.lang.String cname, Namespace ns)
cname
- the name of the childns
- the namespace of the child. A null implies Namespace.NO_NAMESPACE.public java.lang.String getChildTextTrim(java.lang.String cname, Namespace ns)
cname
- the name of the childns
- the namespace of the child. A null implies Namespace.NO_NAMESPACE.public java.lang.String getChildTextNormalize(java.lang.String cname, Namespace ns)
cname
- the name of the childns
- the namespace of the child. A null implies Namespace.NO_NAMESPACE.public Element setText(java.lang.String text)
setContent(java.util.Collection<? extends org.jdom2.Content>)
instead. Setting a null text value is equivalent to setting an empty
string value.text
- new text content for the elementIllegalDataException
- if the assigned text contains an illegal
character such as a vertical tab (as
determined by Verifier.checkCharacterData(java.lang.String)
)public boolean coalesceText(boolean recursively)
recursively
- true if you want the text of child elements coalesced too. False
if you only want to coalesce this Element's Text.public java.util.List<Content> getContent()
Text
, Element
,
Comment
, ProcessingInstruction
,
CDATA
, and EntityRef
.
The List returned is "live" in document order and modifications
to it affect the element's actual contents. Whitespace content is
returned in its entirety.
Sequential traversal through the List is best done with an Iterator since the underlying implement of List.size() may require walking the entire list.
getContent
in interface Parent
List
containing the mixed content of the
element: may contain Text
,
Element
, Comment
,
ProcessingInstruction
,
CDATA
, and
EntityRef
objects.public <E extends Content> java.util.List<E> getContent(Filter<E> filter)
Element
's content.
Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may require walking the entire list.
getContent
in interface Parent
E
- The Generic type of the returned content (the Filter's type)filter
- Filter
to apply
Note that the Filters
class has a number of predefined, useful
filters.List
- filtered Element contentpublic java.util.List<Content> removeContent()
removeContent
in interface Parent
public <F extends Content> java.util.List<F> removeContent(Filter<F> filter)
removeContent
in interface Parent
F
- The Generic type of the content to remove.filter
- filter to select which content to remove
Note that the Filters
class has a number of predefined, useful
filters.public Element setContent(java.util.Collection<? extends Content> newContent)
Element
, Text
,
CDATA
, Comment
,
ProcessingInstruction
, and EntityRef
.
When all objects in the supplied List are legal and before the new
content is added, all objects in the old content will have their
parentage set to null (no parent) and the old content list will be
cleared. This has the effect that any active list (previously obtained
with a call to getContent()
or getChildren()
) will also
change to reflect the new content. In addition, all objects in the
supplied List will have their parentage set to this element, but the
List itself will not be "live" and further removals and additions will
have no effect on this elements content. If the user wants to continue
working with a "live" list, then a call to setContent should be
followed by a call to getContent()
or getChildren()
to
obtain a "live" version of the content.
Passing a null or empty List clears the existing content.
In event of an exception the original content will be unchanged and the objects in the supplied content will be unaltered.
newContent
- Collection
of content to setIllegalAddException
- if the List contains objects of
illegal types or with existing parentage.public Element setContent(int index, Content child)
In event of an exception the original content will be unchanged and the supplied child will be unaltered.
index
- - index of child to replace.child
- - child to add.IllegalAddException
- if the supplied child is already attached
or not legal content for this parent.java.lang.IndexOutOfBoundsException
- if index is negative or greater
than the current number of children.public Parent setContent(int index, java.util.Collection<? extends Content> newContent)
In event of an exception the original content will be unchanged and the content in the supplied collection will be unaltered.
index
- - index of child to replace.newContent
- - Collection
of content to replace child.IllegalAddException
- if the collection contains objects of
illegal types.java.lang.IndexOutOfBoundsException
- if index is negative or greater
than the current number of children.public Element addContent(java.lang.String str)
setText()
.str
- String
to addIllegalDataException
- if str
contains an
illegal character such as a vertical tab (as determined
by Verifier.checkCharacterData(java.lang.String)
)public Element addContent(Content child)
addContent
in interface Parent
child
- child to append to end of content listIllegalAddException
- if the given child already has a parent.public Element addContent(java.util.Collection<? extends Content> newContent)
addContent
in interface Parent
newContent
- Collection
of content to appendIllegalAddException
- if any item in the collection
already has a parent or is of an inappropriate type.public Element addContent(int index, Content child)
addContent
in interface Parent
index
- location for adding the collectionchild
- child to insertjava.lang.IndexOutOfBoundsException
- if index is negative or beyond
the current number of childrenIllegalAddException
- if the given child already has a parent.public Element addContent(int index, java.util.Collection<? extends Content> newContent)
addContent
in interface Parent
index
- location for adding the collectionnewContent
- Collection
of content to insertjava.lang.IndexOutOfBoundsException
- if index is negative or beyond
the current number of childrenIllegalAddException
- if any item in the collection
already has a parent or is of an inappropriate type.public java.util.List<Content> cloneContent()
Parent
cloneContent
in interface Parent
public Content getContent(int index)
Parent
getContent
in interface Parent
index
- location of desired childpublic boolean removeContent(Content child)
Parent
removeContent
in interface Parent
child
- child to removepublic Content removeContent(int index)
Parent
removeContent
in interface Parent
index
- index of child to removepublic Element setContent(Content child)
If the supplied child is legal content for this parent and before it is added, all content in the current content list will be cleared and all current children will have their parentage set to null.
This has the effect that any active list (previously obtained with
a call to one of the getContent()
methods will also change
to reflect the new content. In addition, all content in the supplied
collection will have their parentage set to this parent. If the user
wants to continue working with a "live" list of this parent's
child, then a call to setContent should be followed by a call to one
of the getContent()
methods to obtain a "live"
version of the children.
Passing a null child clears the existing content.
In event of an exception the original content will be unchanged and the supplied child will be unaltered.
child
- new content to replace existing contentIllegalAddException
- if the supplied child is already attached
or not legal content for an Elementpublic boolean isAncestor(Element element)
element
- Element
to check againsttrue
if this element is the ancestor of the
supplied elementpublic boolean hasAttributes()
public boolean hasAdditionalNamespaces()
getAdditionalNamespaces()
because calling getAttributes() will
create an unnecessary List even if there are no Additional Namespaces
attached to the Element. Calling this method first can save memory and
time.public java.util.List<Attribute> 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.
hasAttributes()
or getAttributesSize()
if you just want to see whether there are attributes. Calling this method
may be inefficient if there are no Attributes.public int getAttributesSize()
public Attribute getAttribute(java.lang.String attname)
This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.
attname
- name of the attribute to returnpublic Attribute getAttribute(java.lang.String attname, 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.
attname
- name of the attribute to returnns
- Namespace
to search within. A null implies Namespace.NO_NAMESPACE.public java.lang.String getAttributeValue(java.lang.String 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.
attname
- name of the attribute whose value to be returnedpublic java.lang.String getAttributeValue(java.lang.String attname, java.lang.String 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.
attname
- name of the attribute whose value to be returneddef
- a default value to return if the attribute does not existpublic java.lang.String getAttributeValue(java.lang.String attname, 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.
attname
- name of the attribute whose valud is to be returnedns
- Namespace
to search within. A null implies Namespace.NO_NAMESPACE.public java.lang.String getAttributeValue(java.lang.String attname, Namespace ns, java.lang.String 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.
attname
- name of the attribute whose valud is to be returnedns
- Namespace
to search within. A null implies Namespace.NO_NAMESPACE.def
- a default value to return if the attribute does not existpublic Element setAttributes(java.util.Collection<? extends Attribute> newAttributes)
This sets the attributes of the element. The supplied Collection should
contain only objects of type Attribute
.
When all objects in the supplied List are legal and before the new
attributes are added, all old attributes will have their
parentage set to null (no parent) and the old attribute list will be
cleared. This has the effect that any active attribute list (previously
obtained with a call to getAttributes()
) will also change to
reflect the new attributes. In addition, all attributes in the supplied
List will have their parentage set to this element, but the List itself
will not be "live" and further removals and additions will have no
effect on this elements attributes. If the user wants to continue
working with a "live" attribute list, then a call to setAttributes
should be followed by a call to getAttributes()
to obtain a
"live" version of the attributes.
Passing a null or empty List clears the existing attributes.
In cases where the List contains duplicate attributes, only the last
one will be retained. This has the same effect as calling
setAttribute(Attribute)
sequentially.
In event of an exception the original attributes will be unchanged and the attributes in the supplied attributes will be unaltered.
newAttributes
- Collection
of attributes to setIllegalAddException
- if the List contains objects
that are not instances of Attribute
,
or if any of the Attribute
objects have
conflicting namespace prefixes.public Element setAttribute(java.lang.String name, java.lang.String value)
This sets an attribute value for this element. Any existing attribute with the same name and namespace URI is removed.
name
- name of the attribute to setvalue
- value of the attribute to setIllegalNameException
- if the given name is illegal as an
attribute name.IllegalDataException
- if the given attribute value is
illegal character data (as determined by
Verifier.checkCharacterData(java.lang.String)
).public Element setAttribute(java.lang.String name, java.lang.String value, Namespace ns)
This sets an attribute value for this element. Any existing attribute with the same name and namespace URI is removed.
name
- name of the attribute to setvalue
- value of the attribute to setns
- namespace of the attribute to set. A null implies Namespace.NO_NAMESPACE.IllegalNameException
- if the given name is illegal as an
attribute name, or if the namespace is an unprefixed default
namespaceIllegalDataException
- if the given attribute value is
illegal character data (as determined by
Verifier.checkCharacterData(java.lang.String)
).IllegalAddException
- if the attribute namespace prefix
collides with another namespace prefix on the element.public Element setAttribute(Attribute attribute)
This sets an attribute value for this element. Any existing attribute with the same name and namespace URI is removed.
attribute
- Attribute
to setIllegalAddException
- if the attribute being added already has a
parent or if the attribute namespace prefix collides with another
namespace prefix on the element.public boolean removeAttribute(java.lang.String attname)
This removes the attribute with the given name and within no namespace. If no such attribute exists, this method does nothing.
attname
- name of attribute to removepublic boolean removeAttribute(java.lang.String attname, Namespace ns)
This removes the attribute with the given name and within the given Namespace. If no such attribute exists, this method does nothing.
attname
- name of attribute to removens
- namespace URI of attribute to remove. A null implies Namespace.NO_NAMESPACE.public boolean removeAttribute(Attribute attribute)
This removes the supplied Attribute should it exist.
attribute
- Reference to the attribute to be removed.public java.lang.String toString()
This returns a String
representation of the
Element
, suitable for debugging. If the XML
representation of the Element
is desired,
XMLOutputter.outputString(Element)
should be used.
toString
in class java.lang.Object
String
- information about the
Element
public Element clone()
This returns a deep clone of this element. The new element is detached from its parent, and getParent() on the clone will return null.
public IteratorIterable<Content> getDescendants()
getDescendants
in interface Parent
public <F extends Content> IteratorIterable<F> getDescendants(Filter<F> filter)
getDescendants
in interface Parent
F
- The generic type of the returned descendant datafilter
- filter to select which descendants to see
Note that the Filters
class has a number of predefined, useful
filters.public java.util.List<Element> getChildren()
List
of all the child elements
nested directly (one level deep) within this element, as
Element
objects. If this target element has no nested
elements, an empty List is returned. The returned list is "live"
in document order and changes to it affect the element's actual
contents.
Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may not be the most efficient.
No recursion is performed, so elements nested two levels deep would have to be obtained with:
for(Element oneLevelDeep : topElement.getChildren()) {
List<Element> twoLevelsDeep = oneLevelDeep.getChildren();
// Do something with these children
}
Element
objects for this elementpublic java.util.List<Element> getChildren(java.lang.String cname)
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. If this target element has no nested
elements with the given name outside a namespace, an empty List
is returned. The returned list is "live" in document order
and changes to it affect the element's actual contents.
Please see the notes for
for a code example.
getChildren()
cname
- local name for the children to matchpublic java.util.List<Element> getChildren(java.lang.String cname, Namespace ns)
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. If this target element has no nested
elements with the given name in the given Namespace, an empty List
is returned. The returned list is "live" in document order
and changes to it affect the element's actual contents.
Please see the notes for
for a code example.
getChildren()
cname
- local name for the children to matchns
- Namespace
to search within. A null implies Namespace.NO_NAMESPACE.public Element getChild(java.lang.String cname, Namespace ns)
cname
- local name of child element to match. A null implies any namens
- Namespace
to search within. A null implies any namespace.public Element getChild(java.lang.String cname)
cname
- local name of child element to matchpublic boolean removeChild(java.lang.String cname)
This removes the first child element (one level deep) with the given local name and belonging to no namespace. Returns true if a child was removed.
cname
- the name of child elements to removepublic boolean removeChild(java.lang.String cname, Namespace ns)
This removes the first child element (one level deep) with the given local name and belonging to the given namespace. Returns true if a child was removed.
cname
- the name of child element to removens
- Namespace
to search within. A null implies Namespace.NO_NAMESPACE.public boolean removeChildren(java.lang.String cname)
This removes all child elements (one level deep) with the given local name and belonging to no namespace. Returns true if any were removed.
cname
- the name of child elements to removepublic boolean removeChildren(java.lang.String cname, Namespace ns)
This removes all child elements (one level deep) with the given local name and belonging to the given namespace. Returns true if any were removed.
cname
- the name of child elements to removens
- Namespace
to search within. A null implies Namespace.NO_NAMESPACE.public java.util.List<Namespace> getNamespacesInScope()
The scope is built up from a number of sources following the rules of XML namespace inheritance as follows:
Namespace.XML_NAMESPACE
is added
Namespace.NO_NAMESPACE
)
addNamespaceDeclaration(Namespace)
are included.
Namespace.NO_NAMESPACE
is
included.
Note that the Element'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.
getNamespacesInScope
in interface NamespaceAware
getNamespacesInScope
in class Content
NamespaceAware
public java.util.List<Namespace> getNamespacesInherited()
NamespaceAware
The contents of this list will always be a subset (but in the same order) of getNamespacesInScope(), and will never intersect getNamspacesIntroduced()
getNamespacesInherited
in interface NamespaceAware
getNamespacesInherited
in class Content
public java.util.List<Namespace> getNamespacesIntroduced()
NamespaceAware
The contents of this list will always be a subset (but in the same order) of getNamespacesInScope(), and will never intersect getNamspacesInherited()
getNamespacesIntroduced
in interface NamespaceAware
getNamespacesIntroduced
in class Content
public Element detach()
Content
This method can be overridden by particular Content subclasses to return
a specific type of Content (co-variant return type). All overriding
subclasses must call super.detach()
;
public void canContainContent(Content child, int index, boolean replace) throws IllegalAddException
Parent
canContainContent
in interface Parent
child
- The content to be checkedindex
- The location where the content would be put.replace
- true if the intention is to replace the content already at
the index.IllegalAddException
- if there is a problem with the contentpublic void sortContent(java.util.Comparator<? super Content> comparator)
sortContent(Filter, Comparator)
for
how the algorithm works.
Collections.sort(List, Comparator)
is not appropriate for sorting
the Lists returned from getContent()
because those are
'live' lists, and the Collections.sort() method uses an algorithm that
adds the content in the new location before removing it from the old.
That creates validation issues with content attempting to attach to a
parent before detaching first.
This method provides a safe means to conveniently sort the content.
comparator
- The Comparator to use for the sorting.public void sortChildren(java.util.Comparator<? super Element> comparator)
sortContent(Filter, Comparator)
for how the algorithm works.
Collections.sort(List, Comparator)
is not appropriate for sorting
the Lists returned from getContent()
because those are
'live' lists, and the Collections.sort() method uses an algorithm that
adds the content in the new location before removing it from the old.
This creates validation issues with content attempting to attach to a
parent before detaching first.
This method provides a safe means to conveniently sort the content.
comparator
- The Comparator to use for the sorting.public void sortAttributes(java.util.Comparator<? super Attribute> comparator)
sortContent(Filter, Comparator)
for how the algorithm works.
Collections.sort(List, Comparator)
is not appropriate for sorting
the Lists returned from getContent()
because those are
'live' lists, and the Collections.sort() method uses an algorithm that
adds the content in the new location before removing it from the old.
This creates validation issues with content attempting to attach to a
parent before detaching first.
This method provides a safe means to conveniently sort the content.
A null comparator will sort the Attributes alphabetically first by prefix, then by name
comparator
- The Comparator to use for the sorting.public <E extends Content> void sortContent(Filter<E> filter, java.util.Comparator<? super E> comparator)
The algorithm used for sorting affects the child content in the following ways:
cc
does not match the Filter, then indexOf(cc)
will not be changed by this sort.
Collections.sort(List, Comparator)
is not appropriate for sorting
the Lists returned from getContent()
because those are
'live' lists, and the Collections.sort() method uses an algorithm that
adds the content in the new location before removing it from the old.
This creates validation issues with content attempting to attach to a
parent before detaching first.
This method provides a safe means to conveniently sort the content.
E
- The generic type of the Filter used to select the content to
sort.filter
- The Filter used to select which child content to sort.
Note that the Filters
class has a number of predefined, useful
filters.comparator
- The Comparator to use for the sorting.public java.net.URI getXMLBaseURI() throws java.net.URISyntaxException
This method assumes that all values in xml:base
attributes
are valid URI values according to the java.net.URI
implementation. The same implementation is used to resolve relative URI
values, and thus this code follows the assumptions in java.net.URI.
This technically deviates from the XMLBase spec because to fully support legacy HTML the xml:base attribute could contain what is called a 'LIERI' which is a superset of true URI values, but for practical purposes JDOM users should never encounter such values because they are not processing raw HTML (but xhtml maybe).
java.net.URISyntaxException
- if it is not possible to create java.net.URI values from the data
in the xml:base
attributes.Copyright © 2021 Jason Hunter, Brett McLaughlin. All Rights Reserved.