| |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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.
Constructor Summary | |
Element(String name, Namespace namespace) This will create a new Element
with the supplied (local) name, and define
the Namespace to be used. | |
Element(String name) This will create an Element in no
Namespace . | |
Element(String name, String uri) This will create a new Element with
the supplied (local) name, and specifies the URI
of the Namespace the Element
should be in, resulting it being unprefixed (in the default
namespace). | |
Element(String name, String prefix, String uri) This will create a new Element with
the supplied (local) name, and specifies the prefix and URI
of the Namespace the Element
should be in. |
Method Summary | |
Element | addAttribute(Attribute attribute) This adds an attribute to this element. |
Element | addAttribute(String name, String value) This adds an attribute to this element with the given name and value. |
Element | addContent(String text) This adds text content to this element. |
Element | addContent(Element element) This adds element content to this element. |
Element | addContent(ProcessingInstruction pi) This adds a processing instruction as content to this element. |
Element | addContent(Entity entity) This adds entity content to this element. |
Element | addContent(CDATA cdata) This adds a CDATA section as content to this element. |
Element | addContent(Comment comment) This adds a comment as content to this element. |
void | addNamespaceDeclaration(Namespace additionalNamespace) This will add a namespace declarations to this element. |
Object | clone() This returns a deep clone of this element. |
final boolean | equals(Object ob) This tests for equality of this Element to the supplied
Object , explicitly using the == operator. |
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 getNamespace() . |
Attribute | getAttribute(String name) This returns the attribute for this element with the given name and within no namespace. |
Attribute | getAttribute(String name, Namespace ns) This returns the attribute for this element with the given name and within the given Namespace. |
List | 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. |
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. |
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. |
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. |
Element | getChild(String name) This returns the first child element within this element with the given local name and belonging to no namespace. |
List | getChildren() This returns a List of all the child elements
nested directly (one level deep) within this element, as
Element objects. |
List | getChildren(String name) 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. |
List | getChildren(String name, 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. |
String | getChildText(String name) This convenience method returns the textual content of the named child element, or returns an empty String ("")
if the child has no textual content. |
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. |
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. |
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. |
Element | getCopy(String name, Namespace ns) This creates a copy of this Element , with the new
name specified, and in the specified Namespace . |
Element | getCopy(String name) This creates a copy of this Element , with the new
name specified, and in no namespace. |
Document | getDocument() This retrieves the owning Document for
this Element, or null if not a currently a member of a
Document . |
List | getMixedContent() This returns the full content of the element as a List which may contain objects of type String , Element ,
Comment , ProcessingInstruction , and
Entity . |
String | getName() This returns the (local) name of the Element , without any
namespace prefix, if one exists. |
Namespace | getNamespace() This will return this Element 's
Namespace . |
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). |
String | getNamespacePrefix() This returns the namespace prefix of the Element , if
one exists. |
String | getNamespaceURI() This returns the URI mapped to this Element 's
prefix (or the default namespace if no prefix). |
Element | getParent() This will return the parent of this Element . |
String | getQualifiedName() This returns the full name of the Element , in the form
[namespacePrefix]:[localName]. |
final String | getSerializedForm() This will return the Element in XML format,
usable in an XML document. |
String | getText() This returns the textual content directly held under this element. |
String | getTextTrim() This returns the textual content of this element with all surrounding whitespace removed and internal whitespace normalized to a single space. |
boolean | hasChildren() |
final int | hashCode() This returns the hash code for this Element . |
boolean | hasMixedContent() This will indicate whether the element has mixed content or not. |
boolean | isRootElement() This returns a boolean value indicating
whether this Element is a root
Element for a JDOM Document . |
boolean | removeAttribute(String name, String uri) This removes the attribute with the given name and within the given namespace URI. |
boolean | removeAttribute(String name) This removes the attribute with the given name and within no namespace. |
boolean | removeAttribute(String name, Namespace ns) This removes the attribute with the given name and within the given Namespace. |
boolean | removeChild(String name) This removes the first child element (one level deep) with the given local name and belonging to no namespace. |
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. |
boolean | removeChildren(String name) This removes all child elements (one level deep) with the given local name and belonging to no namespace. |
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. |
boolean | removeChildren() This removes all child elements. |
boolean | removeContent(Element element) This removes the specified Element . |
boolean | removeContent(ProcessingInstruction pi) This removes the specified ProcessingInstruction . |
boolean | removeContent(Entity entity) This removes the specified Entity . |
boolean | removeContent(Comment comment) This removes the specified Comment . |
Element | setAttributes(List attributes) This sets all the attributes for this element to be those in the given List ; all existing attributes are removed. |
Element | setChildren(List children) This sets the content of the element to be the List of Element objects within the supplied List . |
Element | setMixedContent(List mixedContent) This sets the content of the element. |
Element | setText(String text) This sets the content of the element to be the text given. |
String | toString() This returns a String representation of the
Element , suitable for debugging. |
Constructor Detail |
public Element(String name, Namespace namespace)
This will create a new Element
with the supplied (local) name, and define
the Namespace
to be used.
String
name of element.Namespace
to put element in.public Element(String name)
This will create an Element
in no
Namespace
.
String
name of element.public Element(String name, String uri)
This will create a new Element
with
the supplied (local) name, and specifies the URI
of the Namespace
the Element
should be in, resulting it being unprefixed (in the default
namespace).
String
name of element.String
URI for Namespace
element
should be in.public Element(String name, String prefix, String uri)
This will create a new Element
with
the supplied (local) name, and specifies the prefix and URI
of the Namespace
the Element
should be in.
String
name of element.String
URI for Namespace
element
should be in.Method Detail |
public Element addAttribute(Attribute attribute)
This adds an attribute to this element. Any existing attribute with the same name and namespace URI is removed. (TODO: Code the replacement logic.)
Attribute
to addpublic Element addAttribute(String name, String value)
This adds an attribute to this element with the given name and value. To add attributes in namespaces using addAttribute(Attribute).
public Element addContent(String text)
This adds text content to this element. It does not replace the
existing content as does setText()
.
String
to addpublic Element addContent(Element element)
This adds element content to this element.
Element
to addpublic Element addContent(ProcessingInstruction pi)
This adds a processing instruction as content to this element.
ProcessingInstruction
to addpublic Element addContent(Entity entity)
This adds entity content to this element.
Entity
to addpublic Element addContent(CDATA cdata)
This adds a CDATA section as content to this element.
CDATA
to addpublic Element addContent(Comment comment)
This adds a comment as content to this element.
Comment
to addpublic void addNamespaceDeclaration(Namespace additionalNamespace)
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.
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.
public final boolean equals(Object ob)
This tests for equality of this Element
to the supplied
Object
, explicitly using the == operator.
Object
to compare topublic 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
getNamespace()
. If there are no additional
declarations, this returns null
.
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.
public Attribute getAttribute(String name, Namespace ns)
This returns the attribute for this element with the given name and within the given Namespace.
Namespace
to search withinpublic List 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.
The returned list is "live" and changes to it affect the
element's actual attributes.
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.
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.
Namespace
to search withinpublic 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.
Namespace
to search withinpublic 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.
public List getChildren()
This returns a 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"
and changes to it affect the element's actual contents.
This performs no recursion, so elements nested two levels deep would have to be obtained with:
Iterator itr = currentElement.getChildren().iterator();
while (itr.hasNext()) {
Element oneLevelDeep = (Element)nestedElements.next();
List twoLevelsDeep = oneLevelDeep.getChildren();
// Do something with these children
}
Element
objects for this elementpublic List getChildren(String name)
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. 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"
and changes to it affect the element's actual contents.
Please see the notes for getChildren()
for a code example.
public List getChildren(String name, 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. 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"
and changes to it affect the element's actual contents.
Please see the notes for getChildren()
for a code example.
Namespace
to search withinpublic String getChildText(String name)
This convenience method returns the textual content of the named
child element, or returns an empty String
("")
if the child has no textual content. However, if the child does
not exist, null
is returned.
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.
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.
See getTextTrim()
for details of text trimming.
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.
See getTextTrim()
for
details of text trimming.
public Element getCopy(String name, Namespace ns)
This creates a copy of this Element
, with the new
name specified, and in the specified Namespace
.
String
name of new Element
copy.Namespace
to put copy in.Element
copy of this Element
.public Element getCopy(String name)
This creates a copy of this Element
, with the new
name specified, and in no namespace.
String
name of new Element
copy.public Document getDocument()
This retrieves the owning Document
for
this Element, or null if not a currently a member of a
Document
.
Document
owning this Element, or null.public List getMixedContent()
This returns the full content of the element as a List which
may contain objects of type String
, Element
,
Comment
, ProcessingInstruction
, and
Entity
. When there is technically no mixed content and
all contents are of the same type, then all objects returned in the
List will be of the same type. The List returned is "live" and
modifications to it affect the element's actual contents. Whitespace
content is returned in its entirety.
List
containing the mixed content of the
element: may contain String
,
Element
, Comment
,
ProcessingInstruction
, and
Entity
objects.public String getName()
This returns the (local) name of the
Element
, without any
namespace prefix, if one exists.
String
- element name.public Namespace getNamespace()
This will return this Element
's
Namespace
.
Namespace
- Namespace object for this
Element
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.
Namespace
- namespace in scope for the given
prefix on this Element
, or null if none.public String getNamespacePrefix()
This returns the namespace prefix
of the Element
, if
one exists. Otherwise, an empty
String
is returned.
String
- namespace prefix.public String getNamespaceURI()
This returns the URI mapped to this Element
's
prefix (or the default namespace if no prefix). If no
mapping is found, an empty String
is returned.
String
- namespace URI for this
Element
.public Element getParent()
This will return the parent of this Element
.
If there is no parent, then this returns null
.
Also note that on its own, this is not 100% sufficient to
see if the Element
is not in use - this should
be used in tandem with isRootElement()
to determine this.
Element
.public String getQualifiedName()
This returns the full name of the
Element
, in the form
[namespacePrefix]:[localName]. If
no namespace prefix exists for the
Element
, simply the
local name is returned.
String
- full name of element.public final String getSerializedForm()
This will return the Element
in XML format,
usable in an XML document.
String
- the serialized form of the
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
all String nodes returned by getMixedContent(). The call does not
recurse into child elements. If no textual value exists for the
element, an empty String
("") is returned.
public String getTextTrim()
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.
public boolean hasChildren()
public final int hashCode()
This returns the hash code for this Element
.
public boolean hasMixedContent()
This will indicate whether the element has mixed content or not.
Mixed content is when an element contains both textual and
element data within it. When this evaluates to true
,
getMixedContent()
should be used for getting
element data.
boolean
- indicating whether there
is mixed content (both textual data and elements).public boolean isRootElement()
This returns a boolean
value indicating
whether this Element
is a root
Element
for a JDOM Document
.
This should be used in tandem with
getParent()
to determine
if an Element
has no "attachments" to
parents.
boolean
- whether this is a root element.public boolean removeAttribute(String name, String uri)
This removes the attribute with the given name and within the given namespace URI.
public boolean removeAttribute(String name)
This removes the attribute with the given name and within no namespace.
public boolean removeAttribute(String name, Namespace ns)
This removes the attribute with the given name and within the given Namespace.
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.
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.
Namespace
to search withinpublic 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.
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.
Namespace
to search withinpublic boolean removeChildren()
This removes all child elements. Returns true if any were removed.
public boolean removeContent(Element element)
This removes the specified Element
.
Element
to deletepublic boolean removeContent(ProcessingInstruction pi)
This removes the specified ProcessingInstruction
.
ProcessingInstruction
to deletepublic boolean removeContent(Entity entity)
This removes the specified Entity
.
Entity
to deletepublic boolean removeContent(Comment comment)
This removes the specified Comment
.
Comment
to deletepublic Element setAttributes(List attributes)
This sets all the attributes for this element to be those
in the given List
; all existing attributes are removed.
List
of attributes to setpublic Element setChildren(List children)
This sets the content of the element to be the List of
Element
objects within the supplied List
.
All existing element and non-element content of the element is removed.
List
of Element
objects to addpublic Element setMixedContent(List mixedContent)
This sets the content of the element. The passed in List should
contain only objects of type String
, Element
,
Comment
, ProcessingInstruction
, and
Entity
. Passing a null List simply clears the
existing content.
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, use setMixedContent(java.util.List)
instead.
Setting a null text value is equivalent to setting an empty string
value.
public String toString()
This returns a String
representation of the
Element
, suitable for debugging. If the XML
representation of the Element
is desired,
getSerializedForm()
should be used.
String
- information about the
Element
Association Links |
Element
to Class org.jdom.Namespace
{@link Namespace}
of the Element
to Class java.util.LinkedList
{@link Namespace}
declarations on this
elementto Class org.jdom.Element
to Class org.jdom.Document
to Class java.util.List
Element
to Class java.util.LinkedList
Element
| |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |