|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--org.jdom.Element
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 java.util.List |
attributes
The attributes of the Element |
protected java.util.List |
content
The mixed content of the Element |
protected Document |
document
The Document containing this element, if it is the root element |
protected java.lang.String |
name
The local name of the Element |
protected Namespace |
namespace
The of the Element |
protected Element |
parent
The parent of this Element |
| Constructor Summary | |
protected |
Element()
This protected constructor is provided in order to support an Element subclass that wants full control over variable initialization. |
|
Element(java.lang.String name)
This will create an Element in no
. |
|
Element(java.lang.String name,
Namespace namespace)
This will create a new Element
with the supplied (local) name, and define
the to be used. |
|
Element(java.lang.String name,
java.lang.String uri)
This will create a new Element with
the supplied (local) name, and specifies the URI
of the the Element
should be in, resulting it being unprefixed (in the default
namespace). |
|
Element(java.lang.String name,
java.lang.String prefix,
java.lang.String uri)
This will create a new Element with
the supplied (local) name, and specifies the prefix and URI
of the the Element
should be in. |
| Method Summary | |
Element |
addAttribute(Attribute attribute)
This adds an attribute to this element. |
Element |
addAttribute(java.lang.String name,
java.lang.String value)
This adds an attribute to this element with the given name and value. |
Element |
addChild(Comment comment)
Deprecated. Use addContent(Comment) instead |
Element |
addChild(Element element)
Deprecated. Use addContent(Element) instead |
Element |
addChild(Entity entity)
Deprecated. Use addContent(Entity) instead |
Element |
addChild(ProcessingInstruction pi)
Deprecated. Use addContent(ProcessingInstruction) instead |
Element |
addChild(java.lang.String text)
Deprecated. Use addContent(String) instead |
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. |
Element |
addContent(Element element)
This adds element content to this element. |
Element |
addContent(Entity entity)
This adds entity content to this element. |
Element |
addContent(ProcessingInstruction pi)
This adds a processing instruction as content to this element. |
Element |
addContent(java.lang.String text)
This adds text content to this element. |
java.lang.Object |
clone()
This returns a deep clone of this element. |
boolean |
equals(java.lang.Object ob)
This tests for equality of this Element to the supplied
Object, explicitly using the == operator. |
Attribute |
getAttribute(java.lang.String name)
This returns the attribute for this element with the given name and within no namespace. |
Attribute |
getAttribute(java.lang.String name,
Namespace ns)
This returns the attribute for this element with the given name and within the given Namespace. |
java.util.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. |
java.lang.String |
getAttributeValue(java.lang.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. |
java.lang.String |
getAttributeValue(java.lang.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(java.lang.String name)
This returns the first child element within this element with the given local name and belonging to no namespace. |
Element |
getChild(java.lang.String name,
Namespace ns)
This returns the first child element within this element with the given local name and belonging to the given namespace. |
java.util.List |
getChildren()
This returns a List of all the child elements
nested directly (one level deep) within this element, as
Element objects. |
java.util.List |
getChildren(java.lang.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. |
java.util.List |
getChildren(java.lang.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. |
java.lang.String |
getChildText(java.lang.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. |
java.lang.String |
getChildText(java.lang.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. |
java.lang.String |
getChildTextTrim(java.lang.String name)
This convenience method returns the trimmed textual content of the named child element, or returns null if there's no such child. |
java.lang.String |
getChildTextTrim(java.lang.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. |
java.lang.String |
getContent()
Deprecated. Use getTextTrim() instead |
java.lang.String |
getContent(boolean preserveWhitespace)
Deprecated. Use getText() and getTextTrim() instead |
Element |
getCopy(java.lang.String name)
This creates a copy of this Element, with the new
name specified, and in no namespace. |
Element |
getCopy(java.lang.String name,
Namespace ns)
This creates a copy of this Element, with the new
name specified, and in the specified . |
Document |
getDocument()
This retrieves the owning for
this Element, or null if not a currently a member of a
. |
java.util.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. |
java.util.List |
getMixedContent(boolean preserveWhitespace)
Deprecated. Use getMixedContent(), with no arguments, instead |
java.lang.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
. |
java.lang.String |
getNamespacePrefix()
This returns the namespace prefix of the Element, if
one exists. |
java.lang.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. |
java.lang.String |
getQualifiedName()
This returns the full name of the Element, in the form
[namespacePrefix]:[localName]. |
java.lang.String |
getSerializedForm()
This will return the Element in XML format,
usable in an XML document. |
java.lang.String |
getText()
This returns the textual content directly held under this element. |
java.lang.String |
getTextTrim()
This returns the textual content of this element with all surrounding whitespace removed and internal whitespace normalized to a single space. |
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 . |
boolean |
removeAttribute(java.lang.String name)
This removes the attribute with the given name and within no namespace. |
boolean |
removeAttribute(java.lang.String name,
Namespace ns)
This removes the attribute with the given name and within the given Namespace. |
boolean |
removeAttribute(java.lang.String name,
java.lang.String uri)
This removes the attribute with the given name and within the given namespace URI. |
boolean |
removeChild(Comment comment)
Deprecated. Use getMixedContent().remove(comment) instead |
boolean |
removeChild(Element element)
Deprecated. Use getMixedContent().remove(element) instead |
boolean |
removeChild(Entity entity)
Deprecated. Use getMixedContent().remove(entity) instead |
boolean |
removeChild(ProcessingInstruction pi)
Deprecated. Use getMixedContent().remove(pi) instead |
boolean |
removeChild(java.lang.String name)
This removes the first child element (one level deep) with the given local name and belonging to no namespace. |
boolean |
removeChild(java.lang.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()
This removes all child elements. |
boolean |
removeChildren(java.lang.String name)
This removes all child elements (one level deep) with the given local name and belonging to no namespace. |
boolean |
removeChildren(java.lang.String name,
Namespace ns)
This removes all child elements (one level deep) with the given local name and belonging to the given namespace. |
boolean |
removeContent(Comment comment)
Deprecated. Use getMixedContent().remove(comment) instead |
boolean |
removeContent(Element element)
Deprecated. Use getMixedContent().remove(element) instead |
boolean |
removeContent(Entity entity)
Deprecated. Use getMixedContent().remove(entity) instead |
boolean |
removeContent(ProcessingInstruction pi)
Deprecated. Use getMixedContent().remove(pi) instead |
Element |
setAttributes(java.util.List attributes)
This sets all the attributes for this element to be those in the given List; all existing attributes are removed. |
Element |
setChildren(java.util.List children)
This sets the content of the element to be the List of Element objects within the supplied List. |
Element |
setContent(java.lang.String text)
Deprecated. Use setText(String) instead |
protected Element |
setDocument(Document document)
This sets the parent of this element
and makes it the root element. |
Element |
setMixedContent(java.util.List mixedContent)
This sets the content of the element. |
protected Element |
setParent(Element parent)
This will set the parent of this Element. |
Element |
setText(java.lang.String text)
This sets the content of the element to be the text given. |
java.lang.String |
toString()
This returns a String representation of the
Element, suitable for debugging. |
| Methods inherited from class java.lang.Object |
finalize,
getClass,
notify,
notifyAll,
wait,
wait,
wait |
| Field Detail |
protected java.lang.String name
Elementprotected transient Namespace namespace
Namespace of the Elementprotected Element parent
Elementprotected Document document
protected java.util.List attributes
Elementprotected java.util.List content
Element| Constructor Detail |
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.
public Element(java.lang.String name,
Namespace namespace)
This will create a new Element
with the supplied (local) name, and define
the to be used.
Namespace
name - String name of element.public Element(java.lang.String name)
This will create an Element in no
.
Namespace
name - String name of element.
public Element(java.lang.String name,
java.lang.String uri)
This will create a new Element with
the supplied (local) name, and specifies the URI
of the the NamespaceElement
should be in, resulting it being unprefixed (in the default
namespace).
name - String name of element.uri - String URI for Namespace element
should be in.
public Element(java.lang.String name,
java.lang.String prefix,
java.lang.String uri)
This will create a new Element with
the supplied (local) name, and specifies the prefix and URI
of the the NamespaceElement
should be in.
name - String name of element.uri - String URI for Namespace element
should be in.| Method Detail |
public Element getCopy(java.lang.String name,
Namespace ns)
This creates a copy of this Element, with the new
name specified, and in the specified .
Namespace
name - String name of new Element copy.ns - Namespace to put copy in.Element copy of this Element.public Element getCopy(java.lang.String name)
This creates a copy of this Element, with the new
name specified, and in no namespace.
name - String name of new Element copy.public java.lang.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 Elementpublic java.lang.String getNamespacePrefix()
This returns the namespace prefix
of the Element, if
one exists. Otherwise, an empty
String is returned.
String - namespace prefix.public java.lang.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 java.lang.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 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
to determine this.
isRootElement()
Element - parent of this Element.protected Element setParent(Element parent)
This will set the parent of this Element.
parent - Element to be new parent.Element - this Element modified.public boolean isRootElement()
This returns a boolean value indicating
whether this Element is a root
Element for a JDOM .
This should be used in tandem with
Document to determine
if an getParent()Element has no "attachments" to
parents.
boolean - whether this is a root element.protected Element setDocument(Document document)
This sets the parent of this element
and makes it the root element.
Document
document - Document parentDocument this Element modifiedpublic Document getDocument()
This retrieves the owning for
this Element, or null if not a currently a member of a
Document.
Document
Document owning this Element, or null.public java.lang.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 java.lang.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 java.lang.String getChildText(java.lang.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.
name - the name of the childpublic java.lang.String getChildTextTrim(java.lang.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 for details of text trimming.
getTextTrim()
name - the name of the child
public java.lang.String getChildText(java.lang.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.
name - the name of the childns - the namespace of the child
public java.lang.String getChildTextTrim(java.lang.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 for
details of text trimming.
getTextTrim()
name - the name of the childns - the namespace of the childpublic Element setText(java.lang.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 instead.
setMixedContent(java.util.List)
text - new content for the elementpublic 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,
should be used for getting
element data.
getMixedContent()
boolean - indicating whether there
is mixed content (both textual data and elements).public java.util.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 Element setMixedContent(java.util.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.
public java.util.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 an elements nested two levels deep would have to be obtained with:
List nestedElements = currentElement.getChildren();
for (int i=0; i<nestedElements.size(); i++) {
Element oneLevelDeep = (Element)nestedElements.get(i);
List twoLevelsDeep = oneLevelDeep.getChildren();
// Do something with these children
}
Element objects for this elementpublic Element setChildren(java.util.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.
children - List of Element objects to addpublic java.util.List getChildren(java.lang.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
for a code example.
getChildren()
name - local name for the children to match
public java.util.List getChildren(java.lang.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
for a code example.
getChildren()
name - local name for the children to matchns - Namespace to search within
public Element getChild(java.lang.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.
name - local name of child element to matchns - Namespace to search withinpublic Element getChild(java.lang.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.
name - local name of child element to matchpublic Element addContent(java.lang.String text)
This adds text content to this element. It does not replace the
existing content as does setText().
text - String to addpublic Element addContent(Element element)
This adds element content to this element.
element - Element to addpublic Element addContent(ProcessingInstruction pi)
This adds a processing instruction as content to this element.
pi - ProcessingInstruction to addpublic Element addContent(Entity entity)
This adds entity content to this element.
entity - Entity to addpublic Element addContent(CDATA cdata)
This adds a CDATA section as content to this element.
cdata - CDATA to addpublic Element addContent(Comment comment)
This adds a comment as content to this element.
comment - Comment to addpublic boolean removeChild(java.lang.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.
name - the name of child elements to remove
public boolean removeChild(java.lang.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.
name - the name of child element to removens - Namespace to search withinpublic boolean removeChildren(java.lang.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.
name - the name of child elements to remove
public boolean removeChildren(java.lang.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.
name - the name of child elements to removens - Namespace to search withinpublic boolean removeChildren()
This removes all child elements. Returns true if any were removed.
public java.util.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 Attribute getAttribute(java.lang.String name)
This returns the attribute for this element with the given name and within no namespace.
name - name of the attribute to return
public Attribute getAttribute(java.lang.String name,
Namespace ns)
This returns the attribute for this element with the given name and within the given Namespace.
name - name of the attribute to returnns - Namespace to search withinpublic java.lang.String getAttributeValue(java.lang.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.
name - name of the attribute whose value to be returned
public java.lang.String getAttributeValue(java.lang.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.
name - name of the attribute whose valud is to be returnedns - Namespace to search withinpublic Element setAttributes(java.util.List attributes)
This sets all the attributes for this element to be those
in the given List; all existing attributes are removed.
attributes - List of attributes to setpublic 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 - Attribute to add
public Element addAttribute(java.lang.String name,
java.lang.String value)
This adds an attribute to this element with the given name and value. To add attributes in namespaces using addAttribute(Attribute).
name - name of the attribute to addvalue - value of the attribute to add
public boolean removeAttribute(java.lang.String name,
java.lang.String uri)
This removes the attribute with the given name and within the given namespace URI.
name - name of attribute to removeuri - namespace URI of attribute to removepublic boolean removeAttribute(java.lang.String name)
This removes the attribute with the given name and within no namespace.
name - name of attribute to remove
public boolean removeAttribute(java.lang.String name,
Namespace ns)
This removes the attribute with the given name and within the given Namespace.
name - name of attribute to removens - namespace URI of attribute to removepublic final java.lang.String toString()
This returns a String representation of the
Element, suitable for debugging. If the XML
representation of the Element is desired,
should be used.
getSerializedForm()
String - information about the
Elementpublic final java.lang.String getSerializedForm()
This will return the Element in XML format,
usable in an XML document.
String - the serialized form of the
Element.public final boolean equals(java.lang.Object ob)
This tests for equality of this Element to the supplied
Object, explicitly using the == operator.
ob - Object to compare topublic final int hashCode()
This returns the hash code for this Element.
public final java.lang.Object clone()
This returns a deep clone of this element.
public java.lang.String getContent(boolean preserveWhitespace)
public java.lang.String getContent()
public Element setContent(java.lang.String text)
public java.util.List getMixedContent(boolean preserveWhitespace)
public Element addChild(java.lang.String text)
public Element addChild(Element element)
public Element addChild(ProcessingInstruction pi)
public Element addChild(Entity entity)
public Element addChild(Comment comment)
public boolean removeChild(Element element)
public boolean removeChild(ProcessingInstruction pi)
public boolean removeChild(Entity entity)
public boolean removeChild(Comment comment)
public boolean removeContent(Element element)
This removes the specified Element.
child - Element to deletepublic boolean removeContent(ProcessingInstruction pi)
This removes the specified ProcessingInstruction.
child - ProcessingInstruction to deletepublic boolean removeContent(Entity entity)
This removes the specified Entity.
child - Entity to deletepublic boolean removeContent(Comment comment)
This removes the specified Comment.
comment - Comment to delete
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||