Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.jdom.Element
public class Element
extends java.lang.Object
implements Serializable, Cloneable
Element
defines behavior for an XML
element, modeled in Java. Methods allow the user
to obtain the value of the element's textual content,
obtain its attributes, and get its children.
Field Summary | |
protected List |
|
protected org.jdom.AttributeList |
|
protected org.jdom.ContentList |
|
protected String |
|
protected Namespace | |
protected Object |
|
Constructor Summary | |
| |
Method Summary | |
Element |
|
Element |
|
Element |
|
Element |
|
Element |
|
Element |
|
Element |
|
void |
|
Object |
|
Element |
|
boolean |
|
List |
|
Attribute |
|
Attribute |
|
String |
|
String |
|
String |
|
String |
|
List |
|
Element |
|
Element | |
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
List |
|
List |
|
List |
|
List |
|
List |
|
Document |
|
String |
|
Namespace |
|
Namespace |
|
String |
|
String |
|
Element |
|
String |
|
String |
|
String |
|
String |
|
boolean |
|
int |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
void |
|
Element |
|
Element |
|
Element |
|
Element |
|
Element |
|
Element |
|
protected Element |
|
Element |
|
Element |
|
protected Element | |
Element |
|
String |
|
protected List additionalNamespaces
Additionaldeclarations on this element
Namespace
protected org.jdom.AttributeList attributes
The attributes of theElement
.
protected org.jdom.ContentList content
The mixed content of theElement
.
protected String name
The local name of theElement
protected Object parent
Parent element, document, or null if none
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 anElement
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 aNullPointerException
. Therefore, the constructor for these subclasses should call one of the public constructors so variable initialization is handled automatically.
public Element(String name)
This will create anElement
in no.
Namespace
- Parameters:
name
-String
name of element.
public Element(String name, String uri)
This will create a newElement
with the supplied (local) name, and specifies the URI of thethe
Namespace
Element
should be in, resulting it being unprefixed (in the default namespace).
- Parameters:
name
-String
name of element.uri
-String
URI forNamespace
element should be in.
public Element(String name, String prefix, String uri)
This will create a newElement
with the supplied (local) name, and specifies the prefix and URI of thethe
Namespace
Element
should be in.
- Parameters:
name
-String
name of element.uri
-String
URI forNamespace
element should be in.
public Element addContent(String str)
This adds text content to this element. It does not replace the existing content as doessetText()
.
- Parameters:
str
-String
to add
- Returns:
- this element modified
public Element addContent(CDATA cdata)
This adds a CDATA section as content to this element.
- Parameters:
cdata
-CDATA
to add
- Returns:
- this element modified
public Element addContent(Comment comment)
This adds a comment as content to this element.
- Parameters:
comment
-Comment
to add
- Returns:
- this element modified
public Element addContent(Element element)
This adds element content to this element.
- Parameters:
element
-Element
to add
- Returns:
- this element modified
public Element addContent(EntityRef entity)
This adds entity content to this element.
- Parameters:
entity
-EntityRef
to add
- Returns:
- this element modified
public Element addContent(ProcessingInstruction pi)
This adds a processing instruction as content to this element.
- Parameters:
pi
-ProcessingInstruction
to add
- Returns:
- this element modified
public Element addContent(Text text)
This adds text content to this element. It does not replace the existing content as doessetText()
.
- Parameters:
text
-Text
to add
- Returns:
- this element modified
public void addNamespaceDeclaration(Namespace additional)
This will add 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.
- Parameters:
additional
-Namespace
to add.
public Object 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.
- Returns:
- the clone of this element
public Element detach()
This detaches the element from its parent, or does nothing if the element has no parent.
- Returns:
Element
- thisElement
modified.
public final boolean equals(Object ob)
This tests for equality of thisElement
to the suppliedObject
, explicitly using the == operator.
- Parameters:
ob
-Object
to compare to
- Returns:
- whether the elements are equal
public List getAdditionalNamespaces()
This will return any namespace declarations on this element that exist, excluding the namespace of the element itself, which can be obtained through. If there are no additional declarations, this returns an empty list. Note, the returned list is not live, for performance reasons.
getNamespace()
- Returns:
List
- the additional namespace declarations.
public Attribute getAttribute(String name)
This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.
- Parameters:
name
- name of the attribute to return
- Returns:
- attribute for the element
public Attribute getAttribute(String name, 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.
- Parameters:
name
- name of the attribute to returnns
-Namespace
to search within
- Returns:
- attribute for the element
public String getAttributeValue(String name)
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:
name
- name of the attribute whose value to be returned
- Returns:
- the named attribute's value, or null if no such attribute
public String getAttributeValue(String name, 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.
- Parameters:
name
- 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
public String getAttributeValue(String name, 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.
- Parameters:
name
- name of the attribute whose valud is to be returnedns
-Namespace
to search within
- Returns:
- the named attribute's value, or null if no such attribute
public String getAttributeValue(String name, 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.
- Parameters:
name
- name of the attribute whose valud is to be returnedns
-Namespace
to search withindef
- a default value to return if the attribute does not exist
- Returns:
- the named attribute's value, or the default if no such attribute
public List getAttributes()
This returns the complete set of attributes for this element, as aList
ofAttribute
objects in no particular order, or an empty list if there are none. The returned list is "live" and changes to it affect the element's actual attributes.
- Returns:
- attributes for the element
public Element getChild(String name)
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:
name
- local name of child element to match
- Returns:
- the first matching child element, or null if not found
public Element getChild(String name, Namespace ns)
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:
name
- local name of child element to matchns
-Namespace
to search within
- Returns:
- the first matching child element, or null if not found
public String getChildText(String name)
This convenience method returns the textual content of the named child element, or returns an emptyString
("") if the child has no textual content. However, if the child does not exist,null
is returned.
- Parameters:
name
- the name of the child
- Returns:
- text content for the named child, or null if none
public String getChildText(String name, Namespace ns)
This convenience method returns the textual content of the named child element, or returns null if there's no such child.
- Parameters:
name
- the name of the childns
- the namespace of the child
- Returns:
- text content for the named child, or null if none
public String getChildTextNormalize(String name)
This convenience method returns the normalized textual content of the named child element, or returns null if there's no such child. Seefor details of text normalizing.
getTextNormalize()
- Parameters:
name
- the name of the child
- Returns:
- normalized text content for the named child, or null if none
public String getChildTextNormalize(String name, Namespace ns)
This convenience method returns the normalized textual content of the named child element, or returns null if there's no such child. Seefor details of text normalizing.
getTextNormalize()
- Parameters:
name
- the name of the childns
- the namespace of the child
- Returns:
- normalized text content for the named child, or null if none
public String getChildTextTrim(String name)
This convenience method returns the trimmed textual content of the named child element, or returns null if there's no such child. Seefor details of text trimming.
getTextTrim()
- Parameters:
name
- the name of the child
- Returns:
- trimmed text content for the named child, or null if none
public String getChildTextTrim(String name, Namespace ns)
This convenience method returns the trimmed textual content of the named child element, or returns null if there's no such child. Seefor details of text trimming.
getTextTrim()
- Parameters:
name
- the name of the childns
- the namespace of the child
- Returns:
- trimmed text content for the named child, or null if none
public List getChildren()
This returns aList
of all the child elements nested directly (one level deep) within this element, asElement
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:Iterator itr = (currentElement.getChildren()).iterator(); while(itr.hasNext()) { Element oneLevelDeep = (Element)itr.next(); List twoLevelsDeep = oneLevelDeep.getChildren(); // Do something with these children }
- Returns:
- list of child
Element
objects for this element
public List getChildren(String name)
This returns aList
of all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned asElement
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 forfor a code example.
getChildren
- Parameters:
name
- local name for the children to match
- Returns:
- all matching child elements
public List getChildren(String name, Namespace ns)
This returns aList
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 asElement
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 forfor a code example.
getChildren
- Parameters:
name
- local name for the children to matchns
-Namespace
to search within
- Returns:
- all matching child elements
public List 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.
- Returns:
- a
List
containing the mixed content of the element: may containText
,,
Element
,
Comment
,
ProcessingInstruction
, and
CDATA
objects.
EntityRef
public List getContent(Filter filter)
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.
- Parameters:
filter
-Filter
to apply
- Returns:
List
- filtered Element content
public Document getDocument()
This retrieves the owningfor this Element, or null if not a currently a member of a
Document
.
Document
- Returns:
Document
owning this Element, or null.
public String getName()
This returns the (local) name of theElement
, without any namespace prefix, if one exists.
- Returns:
String
- element name.
public Namespace getNamespace()
This will return thisElement
's.
Namespace
- Returns:
Namespace
- Namespace object for thisElement
public Namespace getNamespace(String prefix)
This returns the Namespace in scope on this element for the given prefix (this involves searching up the tree, so the results depend on the current location of the element). It 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:
Namespace
- namespace in scope for the given prefix on thisElement
, or null if none.
public String getNamespacePrefix()
This returns the namespace prefix of theElement
, if one exists. Otherwise, an emptyString
is returned.
- Returns:
String
- namespace prefix.
public String getNamespaceURI()
This returns the URI mapped to thisElement
's prefix (or the default namespace if no prefix). If no mapping is found, an emptyString
is returned.
- Returns:
String
- namespace URI for thisElement
.
public Element getParent()
This will return the parent of thisElement
. If there is no parent, then this returnsnull
. Also note that on its own, this is not 100% sufficient to see if theElement
is not in use - this should be used in tandem withto determine this.
isRootElement()
- Returns:
- parent of this
Element
.
public String getQualifiedName()
This returns the full name of theElement
, in the form [namespacePrefix]:[localName]. If no namespace prefix exists for theElement
, simply the local name is returned.
- Returns:
String
- full name of element.
public String getText()
This returns the textual content directly held under this element. This will include all text within this single element, including whitespace and CDATA sections if they exist. It's essentially the concatenation of allText
andCDATA
nodes returned by getContent(). The call does not recurse into child elements. If no textual value exists for the element, an emptyString
("") is returned.
- Returns:
- text content for this element, or empty string if none
public String getTextNormalize()
This 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
public String getTextTrim()
This 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
public boolean hasChildren()
Test whether this element has a child element. This method can be used before a call togetContent
, which always creates a "live" list, to improve performance.
- Returns:
true
if this element has at least one child element
public final int hashCode()
This returns the hash code for thisElement
.
- Returns:
- inherited hash code
public boolean isAncestor(Element element)
Determines if this element is the ancestor of another element.
- Parameters:
element
-Element
to check against
- Returns:
true
if this element is the ancestor of the supplied element
public boolean isRootElement()
This returns aboolean
value indicating whether thisElement
is a rootElement
for a JDOM. This should be used in tandem with
Document
to determine if an
getParent()
Element
has no "attachments" to parents.
- Returns:
boolean
- whether this is a root element.
public boolean removeAttribute(String name)
This removes the attribute with the given name and within no namespace. If no such attribute exists, this method does nothing.
- Parameters:
name
- name of attribute to remove
- Returns:
- whether the attribute was removed
public boolean removeAttribute(String name, Namespace ns)
This removes the attribute with the given name and within the given Namespace. If no such attribute exists, this method does nothing.
- Parameters:
name
- name of attribute to removens
- namespace URI of attribute to remove
- Returns:
- whether the attribute was removed
public boolean removeAttribute(Attribute attribute)
This removes the supplied Attribute should it exist.
- Parameters:
attribute
- Reference to the attribute to be removed.
- Returns:
- whether the attribute was removed
public boolean removeChild(String name)
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:
name
- the name of child elements to remove
- Returns:
- whether deletion occurred
public boolean removeChild(String name, 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.
- Parameters:
name
- the name of child element to removens
-Namespace
to search within
- Returns:
- whether deletion occurred
public boolean removeChildren()
This removes all child elements. Returns true if any were removed.
- Returns:
- whether deletion occurred
public boolean removeChildren(String name)
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:
name
- the name of child elements to remove
- Returns:
- whether deletion occurred
public boolean removeChildren(String name, 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.
- Parameters:
name
- the name of child elements to removens
-Namespace
to search within
- Returns:
- whether deletion occurred
public boolean removeContent(CDATA cdata)
This removes the specifiedCDATA
. If the specifiedCDATA
is not a child of thisElement
, this method does nothing.
- Parameters:
cdata
-CDATA
to delete
- Returns:
- whether deletion occurred
public boolean removeContent(Comment comment)
This removes the specifiedComment
. If the specifiedComment
is not a child of thisElement
, this method does nothing.
- Parameters:
comment
-Comment
to delete
- Returns:
- whether deletion occurred
public boolean removeContent(Element element)
This removes the specifiedElement
. If the specifiedElement
is not a child of thisElement
, this method does nothing.
- Parameters:
- Returns:
- whether deletion occurred
public boolean removeContent(EntityRef entity)
This removes the specifiedEntityRef
. If the specifiedEntityRef
is not a child of thisElement
, this method does nothing.
- Parameters:
- Returns:
- whether deletion occurred
public boolean removeContent(ProcessingInstruction pi)
This removes the specifiedProcessingInstruction
. If the specifiedProcessingInstruction
is not a child of thisElement
, this method does nothing.
- Parameters:
- Returns:
- whether deletion occurred
public boolean removeContent(Text text)
This removes the specifiedText
. If the specifiedText
is not a child of thisElement
, this method does nothing.
- Parameters:
text
-Text
to delete
- Returns:
- whether deletion occurred
public void removeNamespaceDeclaration(Namespace additionalNamespace)
This will remove a 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.
public Element setAttribute(String name, String value)
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
public Element setAttribute(String name, String value, Namespace ns)
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
- Returns:
- this element modified
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.
- Parameters:
attribute
-Attribute
to set
- Returns:
- this element modified
public Element setAttributes(List newAttributes)
This sets the attributes of the element. The supplied List should contain only objects of typeAttribute
. 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 togetAttributes()
) 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 callingsetAttribute(Attribute)
sequentially. In event of an exception the original attributes will be unchanged and the attributes in the supplied attributes will be unaltered.
- Parameters:
- Returns:
- this element modified
public Element setChildren(List children)
This sets the content of the element the same assetContent(List)
, except onlyElement
objects are allowed in the supplied list.
- Parameters:
children
-List
ofElement
objects to add
- Returns:
- this element modified
public Element setContent(List newContent)
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 togetContent
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
-List
of content to set
- Returns:
- this element modified
protected Element setDocument(Document document)
This sets theparent of this element and makes it the root element. The caller is responsible for ensuring the element doesn't have a pre-existing parent.
Document
- Parameters:
document
-Document
parent
- Returns:
Document
thisElement
modified
public Element setName(String name)
This sets the (local) name of theElement
.
- Returns:
Element
- the element modified.
public Element setNamespace(Namespace namespace)
This sets thisElement
's. If the provided namespace is null, the element will have no namespace.
Namespace
- Returns:
Element
- the element modified.
protected Element setParent(Element parent)
This will set the parent of thisElement
. The caller is responsible for handling pre-existing parentage.
- Parameters:
parent
-Element
to be new parent.
- Returns:
Element
- thisElement
modified.
public Element setText(String text)
This 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(List)
- Parameters:
text
- new content for the element
- Returns:
- this element modified
public String toString()
This returns aString
representation of theElement
, suitable for debugging. If the XML representation of theElement
is desired,XMLOutputter.outputString(Element)
should be used.
- Returns:
String
- information about theElement