Class Element
- All Implemented Interfaces:
Serializable, Cloneable, NamespaceAware, Parent
- Direct Known Subclasses:
LocatedElement
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.
- Author:
- 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:
-
Nested Class Summary
Nested classes/interfaces inherited from class Content
Content.CType -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedElement()This protected constructor is provided in order to support an Element subclass that wants full control over variable initialization.Create a new element with the supplied (local) name and no namespace.Creates a new element with the supplied (local) name and a namespace given by a URI.Creates a new element with the supplied (local) name and a namespace given by the supplied prefix and URI combination.Creates a new element with the supplied (local) name and namespace. -
Method Summary
Modifier and TypeMethodDescriptionaddContent(int index, Collection<? extends Content> newContent) Inserts the content in a collection into the content list at the given index.addContent(int index, Content child) Inserts the child into the content list at the given index.addContent(String str) This adds text content to this element.addContent(Collection<? extends Content> newContent) Appends all children in the given collection to the end of the content list.addContent(Content child) Appends the child to the end of the element's content list.booleanaddNamespaceDeclaration(Namespace additionalNamespace) Adds a namespace declarations to this element.voidcanContainContent(Content child, int index, boolean replace) Test whether this Parent instance can contain the specified content at the specified position.clone()This returns a deep clone of this element.Returns a list containing detached clones of this parent's content list.booleancoalesceText(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).detach()Detaches this child from its parent or does nothing if the child has no parent.Returns a list of the additional namespace declarations on this element.getAttribute(String attname) This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.getAttribute(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.This returns the complete set of attributes for this element, as aListofAttributeobjects in no particular order, or an empty list if there are none.intGet the number of Attributes currently attached to this Element.getAttributeValue(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.getAttributeValue(String attname, 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.getAttributeValue(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.getAttributeValue(String attname, Namespace ns, 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.This returns the first child element within this element with the given local name and belonging to no namespace.This returns the first child element within this element with the given local name and belonging to the given namespace.This returns aListof all the child elements nested directly (one level deep) within this element, asElementobjects.getChildren(String cname) This returns aListof all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned asElementobjects.getChildren(String cname, Namespace ns) This returns aListof all the child elements nested directly (one level deep) within this element with the given local name and belonging to the given Namespace, returned asElementobjects.getChildText(String cname) Returns the textual content of the named child element, or null if there's no such child.getChildText(String cname, Namespace ns) Returns the textual content of the named child element, or null if there's no such child.getChildTextNormalize(String cname) Returns the normalized textual content of the named child element, or null if there's no such child.getChildTextNormalize(String cname, Namespace ns) Returns the normalized textual content of the named child element, or null if there's no such child.getChildTextTrim(String cname) Returns the trimmed textual content of the named child element, or null if there's no such child.getChildTextTrim(String cname, Namespace ns) Returns the trimmed textual content of the named child element, or null if there's no such child.This returns the full content of the element as a List which may contain objects of typeText,Element,Comment,ProcessingInstruction,CDATA, andEntityRef.getContent(int index) Returns the child at the given index.getContent(Filter<E> filter) Return a filter view of thisElement's content.intReturns the number of children in this parent's content list.Returns an iterator that walks over all descendants in document order.<F extends Content>
IteratorIterable<F> 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.getName()Returns the (local) name of the element (without any namespace prefix).Returns the element'sNamespace.getNamespace(String prefix) Returns theNamespacecorresponding to the given prefix in scope for this element.Returns the namespace prefix of the element or an empty string if none exists.Obtain a list of all namespaces that are in scope for this content, but were not introduced by this content.Get the Namespaces that are in-scope on this Element.Obtain a list of all namespaces that are introduced to the XML tree by this node.Returns the namespace URI mapped to this element's prefix (or the in-scope default namespace URI if no prefix).Returns the full name of the element, in the form [namespacePrefix]:[localName].getText()Returns the textual content directly held under this element as a string.Returns the textual content of this element with all surrounding whitespace removed and internal whitespace normalized to a single space.Returns the textual content of this element with all surrounding whitespace removed.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.)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.booleanIndicate whether this Element has any additional Namespace declarations.booleanIndicate whether this Element has any attributes.intReturns the index of the supplied child in the content list, or -1 if not a child of this parent.booleanisAncestor(Element element) Determines if this element is the ancestor of another element.booleanReturns whether this element is a root element.booleanremoveAttribute(String attname) This removes the attribute with the given name and within no namespace.booleanremoveAttribute(String attname, Namespace ns) This removes the attribute with the given name and within the given Namespace.booleanremoveAttribute(Attribute attribute) This removes the supplied Attribute should it exist.booleanremoveChild(String cname) This removes the first child element (one level deep) with the given local name and belonging to no namespace.booleanremoveChild(String cname, Namespace ns) This removes the first child element (one level deep) with the given local name and belonging to the given namespace.booleanremoveChildren(String cname) This removes all child elements (one level deep) with the given local name and belonging to no namespace.booleanremoveChildren(String cname, Namespace ns) This removes all child elements (one level deep) with the given local name and belonging to the given namespace.Removes all child content from this parent.removeContent(int index) Removes and returns the child at the given index, or returns null if there's no such child.booleanremoveContent(Content child) Removes a single child node from the content list.removeContent(Filter<F> filter) Remove all child content from this parent matching the supplied filter.voidremoveNamespaceDeclaration(Namespace additionalNamespace) Removes an additional namespace declarations from this element.setAttribute(String name, String value) This sets an attribute value for this element.setAttribute(String name, String value, Namespace ns) This sets an attribute value for this element.setAttribute(Attribute attribute) This sets an attribute value for this element.setAttributes(Collection<? extends Attribute> newAttributes) This sets the attributes of the element.setContent(int index, Collection<? extends Content> newContent) Replace the child at the given index with the supplied collection.setContent(int index, Content child) Replace the current child the given index with the supplied child.setContent(Collection<? extends Content> newContent) This sets the content of the element.setContent(Content child) Set this element's content to be the supplied child.Sets the (local) name of the element.setNamespace(Namespace namespace) Sets the element'sNamespace.Sets the content of the element to be the text given.voidsortAttributes(Comparator<? super Attribute> comparator) Sort the Attributes of this Element using a mechanism that is safe for JDOM.voidsortChildren(Comparator<? super Element> comparator) Sort the child Elements of this Element using a mechanism that is safe for JDOM content.voidsortContent(Comparator<? super Content> comparator) Sort the contents of this Element using a mechanism that is safe for JDOM content.<E extends Content>
voidsortContent(Filter<E> filter, Comparator<? super E> comparator) Sort the child content of this Element that matches the Filter, using a mechanism that is safe for JDOM content.toString()This returns aStringrepresentation of theElement, suitable for debugging.Methods inherited from class Content
equals, getCType, getDocument, getParent, getParentElement, hashCode, setParentMethods inherited from interface Parent
getDocument, getParent
-
Field Details
-
name
The local name of the element -
namespace
The namespace of the element
-
-
Constructor Details
-
Element
protected Element()This protected constructor is provided in order to support an Element subclass that wants full control over variable initialization. It intentionally leaves all instance variables null, allowing a lightweight subclass implementation. The subclass is responsible for ensuring all the get and set methods on Element behave as documented.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.
-
Element
Creates a new element with the supplied (local) name and namespace. If the provided namespace is null, the element will have no namespace.- Parameters:
name- local name of the elementnamespace- namespace for the element- Throws:
IllegalNameException- if the given name is illegal as an element name
-
Element
Create a new element with the supplied (local) name and no namespace.- Parameters:
name- local name of the element- Throws:
IllegalNameException- if the given name is illegal as an element name.
-
Element
Creates a new element with the supplied (local) name and a namespace given by a URI. The element will be put into the unprefixed (default) namespace.- Parameters:
name- name of the elementuri- namespace URI for the element- Throws:
IllegalNameException- if the given name is illegal as an element name or the given URI is illegal as a namespace URI
-
Element
Creates a new element with the supplied (local) name and a namespace given by the supplied prefix and URI combination.- Parameters:
name- local name of the elementprefix- namespace prefixuri- namespace URI for the element- Throws:
IllegalNameException- 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 URI
-
-
Method Details
-
getName
Returns the (local) name of the element (without any namespace prefix).- Returns:
- local element name
-
setName
Sets the (local) name of the element.- Parameters:
name- the new (local) name of the element- Returns:
- the target element
- Throws:
IllegalNameException- if the given name is illegal as an Element name
-
getNamespace
-
setNamespace
Sets the element'sNamespace. If the provided namespace is null, the element will have no namespace.- Parameters:
namespace- the new namespace. A null implies Namespace.NO_NAMESPACE.- Returns:
- the target element
- Throws:
IllegalAddException- if there is a Namespace conflict
-
getNamespacePrefix
Returns the namespace prefix of the element or an empty string if none exists.- Returns:
- the namespace prefix
-
getNamespaceURI
Returns the namespace URI mapped to this element's prefix (or the in-scope default namespace URI if no prefix). If no mapping is found, an empty string is returned.- Returns:
- the namespace URI for this element
-
getNamespace
Returns theNamespacecorresponding 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.- Parameters:
prefix- namespace prefix to look up- Returns:
- the Namespace for this prefix at this location, or null if none
-
getQualifiedName
Returns the full name of the element, in the form [namespacePrefix]:[localName]. If the element does not have a namespace prefix, then the local name is returned.- Returns:
- qualified name of the element (including namespace prefix)
-
addNamespaceDeclaration
Adds a namespace declarations to this element. This should not be used to add the declaration for this element itself; that should be assigned in the construction of the element. Instead, this is for adding namespace declarations on the element not relating directly to itself. It's used during output to for stylistic reasons move namespace declarations higher in the tree than they would have to be.- Parameters:
additionalNamespace- namespace to add- Returns:
- true if the namespace is added (false if it was previously added)
- Throws:
IllegalAddException- if the namespace prefix collides with another namespace prefix on the element
-
removeNamespaceDeclaration
Removes an additional namespace declarations from this element. This should not be used to remove the declaration for this element itself; that should be handled in the construction of the element. Instead, this is for removing namespace declarations on the element not relating directly to itself. If the declaration is not present, this method does nothing.- Parameters:
additionalNamespace- namespace to remove. A null Namespace does nothing.
-
getAdditionalNamespaces
Returns a list of the additional namespace declarations on this element. This includes only additional namespace, not the namespace of the element itself, which can be obtained throughgetNamespace(). If there are no additional declarations, this returns an empty list. Note, the returned list is unmodifiable.- Returns:
- a List of the additional namespace declarations
-
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.) -
isRootElement
public boolean isRootElement()Returns whether this element is a root element. This can be used in tandem withContent.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.
- Returns:
- whether this is a root element
-
getContentSize
public int getContentSize()Description copied from interface:ParentReturns the number of children in this parent's content list. Children may be anyContenttype.- Specified by:
getContentSizein interfaceParent- Returns:
- number of children
-
indexOf
-
getText
Returns the textual content directly held under this element as a string. This includes all text within this single element, including whitespace and CDATA sections if they exist. It's essentially the concatenation of allTextandCDATAnodes returned bygetContent(). The call does not recurse into child elements. If no textual value exists for the element, an empty string is returned.- Returns:
- text content for this element, or empty string if none
-
getTextTrim
Returns the textual content of this element with all surrounding whitespace removed. If no textual value exists for the element, or if only whitespace exists, the empty string is returned.- Returns:
- trimmed 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. If no textual value exists for the element, or if only whitespace exists, the empty string is returned.- Returns:
- normalized text content for this element, or empty string if none
-
getChildText
Returns the textual content of the named child element, or null if there's no such child. This method is a convenience because callinggetChild().getText()can throw a NullPointerException.- Parameters:
cname- the name of the child- Returns:
- text content for the named child, or null if no such child
-
getChildTextTrim
Returns the trimmed textual content of the named child element, or null if there's no such child. Seefor details of text trimming.getTextTrim()- Parameters:
cname- the name of the child- Returns:
- trimmed text content for the named child, or null if no such child
-
getChildTextNormalize
Returns the normalized textual content of the named child element, or null if there's no such child. Seefor details of text normalizing.getTextNormalize()- Parameters:
cname- the name of the child- Returns:
- normalized text content for the named child, or null if no such child
-
getChildText
Returns the textual content of the named child element, or null if there's no such child.- Parameters:
cname- the name of the childns- the namespace of the child. A null implies Namespace.NO_NAMESPACE.- Returns:
- text content for the named child, or null if no such child
-
getChildTextTrim
Returns the trimmed textual content of the named child element, or null if there's no such child.- Parameters:
cname- the name of the childns- the namespace of the child. A null implies Namespace.NO_NAMESPACE.- Returns:
- trimmed text content for the named child, or null if no such child
-
getChildTextNormalize
Returns the normalized textual content of the named child element, or null if there's no such child.- Parameters:
cname- the name of the childns- the namespace of the child. A null implies Namespace.NO_NAMESPACE.- Returns:
- normalized text content for the named child, or null if no such child
-
setText
Sets the content of the element to be the text given. All existing text content and non-text context is removed. If this element should have both textual content and nested elements, useinstead. Setting a null text value is equivalent to setting an empty string value.setContent(Collection)- Parameters:
text- new text content for the element- Returns:
- the target element
- Throws:
IllegalDataException- if the assigned text contains an illegal character such as a vertical tab (as determined byVerifier.checkCharacterData(String))
-
coalesceText
public 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).- Parameters:
recursively- true if you want the text of child elements coalesced too. False if you only want to coalesce this Element's Text.- Returns:
- true if any content was changed by this operation.
-
getContent
This returns the full content of the element as a List which may contain objects of typeText,Element,Comment,ProcessingInstruction,CDATA, andEntityRef. 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.
- Specified by:
getContentin interfaceParent- Returns:
- a
Listcontaining the mixed content of the element: may containText,,Element,Comment,ProcessingInstruction, andCDATAobjects.EntityRef
-
getContent
Return a filter view of thisElement'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.
- Specified by:
getContentin interfaceParent- Type Parameters:
E- The Generic type of the returned content (the Filter's type)- Parameters:
filter-Filterto apply Note that theFiltersclass has a number of predefined, useful filters.- Returns:
List- filtered Element content
-
removeContent
Removes all child content from this parent.- Specified by:
removeContentin interfaceParent- Returns:
- list of the old children detached from this parent
-
removeContent
Remove all child content from this parent matching the supplied filter.- Specified by:
removeContentin interfaceParent- Type Parameters:
F- The Generic type of the content to remove.- Parameters:
filter- filter to select which content to remove Note that theFiltersclass has a number of predefined, useful filters.- Returns:
- list of the old children detached from this parent
-
setContent
This sets the content of the element. The supplied List should contain only objects of typeElement,Text,CDATA,Comment,ProcessingInstruction, andEntityRef.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()orgetChildren()) 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 togetContent()orgetChildren()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.
- Parameters:
newContent-Collectionof content to set- Returns:
- this element modified
- Throws:
IllegalAddException- if the List contains objects of illegal types or with existing parentage.
-
setContent
Replace the current child the given index with the supplied child.In event of an exception the original content will be unchanged and the supplied child will be unaltered.
- Parameters:
index- - index of child to replace.child- - child to add.- Returns:
- element on which this method was invoked
- Throws:
IllegalAddException- if the supplied child is already attached or not legal content for this parent.IndexOutOfBoundsException- if index is negative or greater than the current number of children.
-
setContent
Replace the child at the given index with the supplied collection.In event of an exception the original content will be unchanged and the content in the supplied collection will be unaltered.
- Parameters:
index- - index of child to replace.newContent- -Collectionof content to replace child.- Returns:
- object on which this method was invoked
- Throws:
IllegalAddException- if the collection contains objects of illegal types.IndexOutOfBoundsException- if index is negative or greater than the current number of children.
-
addContent
This adds text content to this element. It does not replace the existing content as doessetText().- Parameters:
str-Stringto add- Returns:
- this element modified
- Throws:
IllegalDataException- ifstrcontains an illegal character such as a vertical tab (as determined byVerifier.checkCharacterData(String))
-
addContent
Appends the child to the end of the element's content list.- Specified by:
addContentin interfaceParent- Parameters:
child- child to append to end of content list- Returns:
- the element on which the method was called
- Throws:
IllegalAddException- if the given child already has a parent.
-
addContent
Appends all children in the given collection to the end of the content list. In event of an exception during add the original content will be unchanged and the objects in the supplied collection will be unaltered.- Specified by:
addContentin interfaceParent- Parameters:
newContent-Collectionof content to append- Returns:
- the element on which the method was called
- Throws:
IllegalAddException- if any item in the collection already has a parent or is of an inappropriate type.
-
addContent
Inserts the child into the content list at the given index.- Specified by:
addContentin interfaceParent- Parameters:
index- location for adding the collectionchild- child to insert- Returns:
- the parent on which the method was called
- Throws:
IndexOutOfBoundsException- if index is negative or beyond the current number of childrenIllegalAddException- if the given child already has a parent.
-
addContent
Inserts the content in a collection into the content list at the given index. In event of an exception the original content will be unchanged and the objects in the supplied collection will be unaltered.- Specified by:
addContentin interfaceParent- Parameters:
index- location for adding the collectionnewContent-Collectionof content to insert- Returns:
- the parent on which the method was called
- Throws:
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.
-
cloneContent
Description copied from interface:ParentReturns a list containing detached clones of this parent's content list.- Specified by:
cloneContentin interfaceParent- Returns:
- list of cloned child content
-
getContent
Description copied from interface:ParentReturns the child at the given index.- Specified by:
getContentin interfaceParent- Parameters:
index- location of desired child- Returns:
- child at the given index
-
removeContent
Description copied from interface:ParentRemoves a single child node from the content list.- Specified by:
removeContentin interfaceParent- Parameters:
child- child to remove- Returns:
- whether the removal occurred
-
removeContent
Description copied from interface:ParentRemoves and returns the child at the given index, or returns null if there's no such child.- Specified by:
removeContentin interfaceParent- Parameters:
index- index of child to remove- Returns:
- detached child at given index or null if no
-
setContent
Set this element's content to be the supplied 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 thegetContent()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.
- Parameters:
child- new content to replace existing content- Returns:
- the parent on which the method was called
- Throws:
IllegalAddException- if the supplied child is already attached or not legal content for an Element
-
isAncestor
Determines if this element is the ancestor of another element.- Parameters:
element-Elementto check against- Returns:
trueif this element is the ancestor of the supplied element
-
hasAttributes
public boolean hasAttributes()Indicate whether this Element has any attributes. Where possible you should call this method before calling getAttributes() because calling getAttributes() will create the necessary Attribute List memory structures, even if there are no Attributes attached to the Element. Calling hasAttributes() first can save memory.- Returns:
- true if this Element has attributes.
-
hasAdditionalNamespaces
public boolean hasAdditionalNamespaces()Indicate whether this Element has any additional Namespace declarations. Where possible you should call this method before callinggetAdditionalNamespaces()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.- Returns:
- true if this Element has additional Namespaces.
-
getAttributes
This returns the complete set of attributes for this element, as a
The returned list is "live" and changes to it affect the element's actual attributes. Use the methodsListofAttributeobjects in no particular order, or an empty list if there are none.hasAttributes()orgetAttributesSize()if you just want to see whether there are attributes. Calling this method may be inefficient if there are no Attributes.- Returns:
- attributes for the element
-
getAttributesSize
public int getAttributesSize()Get the number of Attributes currently attached to this Element.- Returns:
- the number of Attributes attached.
-
getAttribute
-
getAttribute
This returns the attribute for this element with the given name and within the given Namespace, or null if no such attribute exists.
- Parameters:
attname- name of the attribute to returnns-Namespaceto search within. A null implies Namespace.NO_NAMESPACE.- Returns:
- attribute for the element
-
getAttributeValue
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.
- Parameters:
attname- name of the attribute whose value to be returned- Returns:
- the named attribute's value, or null if no such attribute
-
getAttributeValue
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.
- Parameters:
attname- name of the attribute whose value to be returneddef- a default value to return if the attribute does not exist- Returns:
- the named attribute's value, or the default if no such attribute
-
getAttributeValue
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.
- Parameters:
attname- name of the attribute whose valud is to be returnedns-Namespaceto search within. A null implies Namespace.NO_NAMESPACE.- Returns:
- the named attribute's value, or null if no such attribute
-
getAttributeValue
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.
- Parameters:
attname- name of the attribute whose valud is to be returnedns-Namespaceto search within. A null implies Namespace.NO_NAMESPACE.def- a default value to return if the attribute does not exist- Returns:
- the named attribute's value, or the default if no such attribute
-
setAttributes
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 togetAttributes()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.
- Parameters:
newAttributes-Collectionof attributes to set- Returns:
- this element modified
- Throws:
IllegalAddException- if the List contains objects that are not instances ofAttribute, or if any of theAttributeobjects have conflicting namespace prefixes.
-
setAttribute
This sets an attribute value for this element. Any existing attribute with the same name and namespace URI is removed.
- Parameters:
name- name of the attribute to setvalue- value of the attribute to set- Returns:
- this element modified
- Throws:
IllegalNameException- if the given name is illegal as an attribute name.IllegalDataException- if the given attribute value is illegal character data (as determined byVerifier.checkCharacterData(String)).
-
setAttribute
This sets an attribute value for this element. Any existing attribute with the same name and namespace URI is removed.
- Parameters:
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.- Returns:
- this element modified
- Throws:
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 byVerifier.checkCharacterData(String)).IllegalAddException- if the attribute namespace prefix collides with another namespace prefix on the element.
-
setAttribute
This sets an attribute value for this element. Any existing attribute with the same name and namespace URI is removed.
- Parameters:
attribute-Attributeto set- Returns:
- this element modified
- Throws:
IllegalAddException- if the attribute being added already has a parent or if the attribute namespace prefix collides with another namespace prefix on the element.
-
removeAttribute
This removes the attribute with the given name and within no namespace. If no such attribute exists, this method does nothing.
- Parameters:
attname- name of attribute to remove- Returns:
- whether the attribute was removed
-
removeAttribute
This removes the attribute with the given name and within the given Namespace. If no such attribute exists, this method does nothing.
- Parameters:
attname- name of attribute to removens- namespace URI of attribute to remove. A null implies Namespace.NO_NAMESPACE.- Returns:
- whether the attribute was removed
-
removeAttribute
This removes the supplied Attribute should it exist.
- Parameters:
attribute- Reference to the attribute to be removed.- Returns:
- whether the attribute was removed
-
toString
This returns a
Stringrepresentation of theElement, suitable for debugging. If the XML representation of theElementis desired,XMLOutputter.outputString(Element)should be used. -
clone
-
getDescendants
Returns an iterator that walks over all descendants in document order.- Specified by:
getDescendantsin interfaceParent- Returns:
- an iterator to walk descendants
-
getDescendants
Returns an iterator that walks over all descendants in document order applying the Filter to return only content that match the filter rule. With filters you can match only Elements, only Comments, Elements or Comments, only Elements with a given name and/or prefix, and so on.- Specified by:
getDescendantsin interfaceParent- Type Parameters:
F- The generic type of the returned descendant data- Parameters:
filter- filter to select which descendants to see Note that theFiltersclass has a number of predefined, useful filters.- Returns:
- an iterator to walk descendants within a filter
-
getChildren
This returns aListof all the child elements nested directly (one level deep) within this element, asElementobjects. 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 }- Returns:
- list of child
Elementobjects for this element
-
getChildren
This returns aListof all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned asElementobjects. 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()- Parameters:
cname- local name for the children to match- Returns:
- all matching child elements
-
getChildren
This returns aListof all the child elements nested directly (one level deep) within this element with the given local name and belonging to the given Namespace, returned asElementobjects. 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()- Parameters:
cname- local name for the children to matchns-Namespaceto search within. A null implies Namespace.NO_NAMESPACE.- Returns:
- all matching child elements
-
getChild
This returns the first child element within this element with the given local name and belonging to the given namespace. If no elements exist for the specified name and namespace, null is returned.- Parameters:
cname- local name of child element to match. A null implies any namens-Namespaceto search within. A null implies any namespace.- Returns:
- the first matching child element, or null if not found
-
getChild
This returns the first child element within this element with the given local name and belonging to no namespace. If no elements exist for the specified name and namespace, null is returned.- Parameters:
cname- local name of child element to match- Returns:
- the first matching child element, or null if not found
-
removeChild
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.
- Parameters:
cname- the name of child elements to remove- Returns:
- whether deletion occurred
-
removeChild
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.
- Parameters:
cname- the name of child element to removens-Namespaceto search within. A null implies Namespace.NO_NAMESPACE.- Returns:
- whether deletion occurred
-
removeChildren
This removes all child elements (one level deep) with the given local name and belonging to no namespace. Returns true if any were removed.
- Parameters:
cname- the name of child elements to remove- Returns:
- whether deletion occurred
-
removeChildren
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.
- Parameters:
cname- the name of child elements to removens-Namespaceto search within. A null implies Namespace.NO_NAMESPACE.- Returns:
- whether deletion occurred
-
getNamespacesInScope
Get the Namespaces that are in-scope on this Element. Element has the most complex rules for the namespaces-in-scope.The scope is built up from a number of sources following the rules of XML namespace inheritance as follows:
- The
Namespace.XML_NAMESPACEis added - The element's namespace is added (commonly
Namespace.NO_NAMESPACE) - All the attributes are inspected and for those that have a namespace prefix then their Namespaces are included (the "default" namespace for attributes is not the same as the "default" namespace for the element that attribute is on).
- All Namespaces declared on this Element using
addNamespaceDeclaration(Namespace)are included. - If the element has a parent then the parent's Namespace scope is inspected, and any prefixes in the parent scope that are not yet bound in this Element's scope are included.
- If the default Namespace (the no-prefix namespace) has not been
encountered for this Element then
Namespace.NO_NAMESPACEis 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
NamespaceAwaredocumentation for details on what the order of the Namespaces will be in the returned list.- Specified by:
getNamespacesInScopein interfaceNamespaceAware- Overrides:
getNamespacesInScopein classContent- Returns:
- a read-only list of Namespaces.
- See Also:
- The
-
getNamespacesInherited
Description copied from interface:NamespaceAwareObtain 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()
- Specified by:
getNamespacesInheritedin interfaceNamespaceAware- Overrides:
getNamespacesInheritedin classContent- Returns:
- a read-only list of Namespaces.
-
getNamespacesIntroduced
Description copied from interface:NamespaceAwareObtain 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()
- Specified by:
getNamespacesIntroducedin interfaceNamespaceAware- Overrides:
getNamespacesIntroducedin classContent- Returns:
- a read-only list of Namespaces.
-
detach
Description copied from class:ContentDetaches this child from its parent or does nothing if the child has no parent.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(); -
canContainContent
Description copied from interface:ParentTest whether this Parent instance can contain the specified content at the specified position.- Specified by:
canContainContentin interfaceParent- Parameters:
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.- Throws:
IllegalAddException- if there is a problem with the content
-
sortContent
Sort the contents of this Element using a mechanism that is safe for JDOM content. See the notes onsortContent(Filter, Comparator)for how the algorithm works.Collections.sort(List, Comparator)is not appropriate for sorting the Lists returned fromgetContent()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.
- Parameters:
comparator- The Comparator to use for the sorting.
-
sortChildren
Sort the child Elements of this Element using a mechanism that is safe for JDOM content. Other child content will be unaffected. See the notes onsortContent(Filter, Comparator)for how the algorithm works.Collections.sort(List, Comparator)is not appropriate for sorting the Lists returned fromgetContent()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.
- Parameters:
comparator- The Comparator to use for the sorting.
-
sortAttributes
Sort the Attributes of this Element using a mechanism that is safe for JDOM. Other child content will be unaffected. See the notes onsortContent(Filter, Comparator)for how the algorithm works.Collections.sort(List, Comparator)is not appropriate for sorting the Lists returned fromgetContent()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
- Parameters:
comparator- The Comparator to use for the sorting.
-
sortContent
Sort the child content of this Element that matches the Filter, using a mechanism that is safe for JDOM content. Other child content (that does not match the filter) will be unaffected.The algorithm used for sorting affects the child content in the following ways:
- Items not matching the Filter will be unchanged. This includes the
absolute position of that content in this Element. i.e. if child content
ccdoes not match the Filter, thenindexOf(cc)will not be changed by this sort. - Items matching the Filter will be reordered according to the comparator. Only the relative order of the Filtered data will change.
- Items that compare as 'equals' using the comparator will keep the the same relative order as before the sort.
Collections.sort(List, Comparator)is not appropriate for sorting the Lists returned fromgetContent()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.
- Type Parameters:
E- The generic type of the Filter used to select the content to sort.- Parameters:
filter- The Filter used to select which child content to sort. Note that theFiltersclass has a number of predefined, useful filters.comparator- The Comparator to use for the sorting.
- Items not matching the Filter will be unchanged. This includes the
absolute position of that content in this Element. i.e. if child content
-
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.This method assumes that all values in
xml:baseattributes are valid URI values according to thejava.net.URIimplementation. 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).
- Returns:
- a URI representing the XMLBase value for the supplied Element, or null if one could not be calculated.
- Throws:
URISyntaxException- if it is not possible to create java.net.URI values from the data in thexml:baseattributes.
-