public class Element extends AbstractGroup implements Attributable, Namespaceable, Element
Attribute,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
protected class |
Element.AttrArray |
protected class |
Element.AttrMap |
AbstractGroup.ChildArray, AbstractGroup.ElementMap| Modifier and Type | Field and Description |
|---|---|
protected Map<String,Namespace> |
_addNamespaces
Additional namespaces.
|
protected List<Attribute> |
_attrs
The attributes.
|
protected String |
_lname
The local name.
|
protected Namespace |
_ns
The namespace.
|
_childrenATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODEFIND_BY_PREFIX, FIND_BY_REGEX, FIND_BY_TAGNAME, FIND_IGNORE_CASE, FIND_RECURSIVE| Modifier | Constructor and Description |
|---|---|
protected |
Element()
Constructor.
|
|
Element(Namespace ns,
String lname)
Constructor.
|
|
Element(String lname)
Constructor without a namespace (i.e.,
Namespace.NO_NAMESPACE). |
|
Element(String nsURI,
String tname)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addDeclaredNamespace(Namespace ns)
Adds a namespace to the namespace declaration.
|
Object |
clone()
Clones this object (a deep cloning not including contents contained
in Textual nodes).
|
String |
getAttribute(String tname) |
int |
getAttributeIndex(int indexFrom,
String tname)
Gets the index of the attribute with the giving local name.
|
int |
getAttributeIndex(int indexFrom,
String namespace,
String name,
int mode)
Gets the index of the first attribute that matches
the specified criteria.
|
Attribute |
getAttributeItem(String tname)
Gets the attribute with the tag name.
|
Attribute |
getAttributeItem(String namespace,
String name,
int mode)
Gets the first attribute that matches the specified criteria.
|
List<Attribute> |
getAttributeItems()
Returns all attributes of this object.
|
Attr |
getAttributeNode(String tname) |
Attr |
getAttributeNodeNS(String nsURI,
String lname) |
String |
getAttributeNS(String nsURI,
String lname) |
NamedNodeMap |
getAttributes() |
List<Attribute> |
getAttributes(String namespace,
String name,
int mode)
Gets a list of attributes of the specified criteria.
|
String |
getAttributeValue(String tname)
Returns the value of the attribute of the specified tag name,
or null if not specified.
|
String |
getAttributeValue(String namespace,
String name,
int mode)
Gets the value of the first attribute that matches
the giving criteria, or null if not found.
|
Object |
getContent()
Gets the content of this element.
|
Object |
getContent(String path)
Returns the content of the child element with the giving path, or
null if the content is null or the child element doesn't exist.
|
Collection<Namespace> |
getDeclaredNamespaces()
Returns namespace declared on this element.
|
NodeList |
getElementsByTagName(String tname) |
NodeList |
getElementsByTagNameNS(String nsURI,
String lname) |
String |
getLocalName()
Gets the local name of this item.
|
String |
getName()
Gets the tag name of the element -- the name with prefix.
|
Namespace |
getNamespace()
Gets the namespace.
|
Namespace |
getNamespace(String prefix)
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), or null if not found.
|
String |
getNamespaceURI() |
short |
getNodeType() |
String |
getNodeValue()
Always null.
|
String |
getPrefix() |
TypeInfo |
getSchemaTypeInfo() |
String |
getTagName()
Gets the tag name of this item.
|
String |
getText()
Returns the concatenation of
Textual children; never null. |
String |
getText(boolean trim)
Returns the concatenation of
Textual children; never null. |
boolean |
hasAttribute(String tname) |
boolean |
hasAttributeNS(String nsURI,
String lname) |
boolean |
hasAttributes() |
boolean |
hasContent(String path)
Tests whether the child element with the giving path exists.
|
boolean |
isRootElement()
Tests whether this element is the root element of
the owning document.
|
protected List<Attribute> |
newAttrArray()
Creates an empty list of attributes.
|
void |
removeAttribute(String tname) |
Attr |
removeAttributeNode(Attr oldAttr) |
void |
removeAttributeNS(String nsURI,
String lname) |
Object |
removeContent(String path)
Removes the content of the child element with the giving path,
and the child element itself if no other child.
|
Attribute |
setAttribute(Attribute attr)
Adds the giving attribute.
|
void |
setAttribute(String tname,
String value) |
Attr |
setAttributeNode(Attr newAttr) |
Attr |
setAttributeNodeNS(Attr newAttr) |
void |
setAttributeNS(String nsURI,
String tname,
String value) |
Attribute |
setAttributeValue(String tname,
String value)
Sets the value of the attribute with the giving tag name.
|
Object |
setContent(Object obj)
Sets the content of this element.
|
Object |
setContent(String path,
Object obj)
Sets the content of the child element with the giving path.
|
void |
setIdAttribute(String name,
boolean isId) |
void |
setIdAttributeNode(Attr idAttr,
boolean isId) |
void |
setIdAttributeNS(String namespaceURI,
String localName,
boolean isId) |
void |
setLocalName(String lname)
Sets the local name of this item.
|
void |
setName(String tname)
Sets the tag name of the element.
|
void |
setNamespace(Namespace ns)
Sets the namespace.
|
void |
setNamespace(String prefix,
String nsURI)
Sets the namespace.
|
void |
setPrefix(String prefix) |
void |
setTagName(String tname)
Sets the tag name of this item.
|
String |
toString() |
anyElement, appendChild, coalesce, detachChildren, getChildNodes, getChildren, getElement, getElement, getElementIndex, getElementIndex, getElementNames, getElements, getElements, getElements, getElementValue, getElementValue, getFirstChild, getLastChild, hasChildNodes, insertBefore, newChildren, removeChild, replaceChildcloneNode, compareDocumentPosition, detach, equals, getBaseURI, getDocument, getFeature, getLocator, getNextSibling, getNodeName, getOwnerDocument, getParent, getParentNode, getPreviousSibling, getTextContent, getUserData, hashCode, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, match, normalize, setLocator, setNodeValue, setParent, setText, setTextContent, setUserDatafinalize, getClass, notify, notifyAll, wait, wait, waitappendChild, cloneNode, compareDocumentPosition, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getNextSibling, getNodeName, getOwnerDocument, getParentNode, getPreviousSibling, getTextContent, getUserData, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setTextContent, setUserDatadetach, getDocument, getLocator, getParent, setLocator, setParent, setTextprotected Namespace _ns
protected String _lname
public Element(String nsURI, String tname)
nsURI - the namespace URItname - the tag namepublic Element(Namespace ns, String lname)
ns - the namespace; if null, the default namespace is assumed
(not necessary Namespace.NO_NAMESPACE).lname - the local namepublic Element(String lname)
Namespace.NO_NAMESPACE).lname - the local nameprotected Element()
public final boolean isRootElement()
Note: false is returned if it doesn't has any parent.
public final Namespace getNamespace(String prefix)
If prefix is empty, it searches for the "default" namespace in scope. Thus, to search for attribute's namespace, caller have to skip this one and use NO_NAMESPACE. (due XML, an attribute without prefix is NO_NAMESPACE)
prefix - namespace prefix to look up; null for emptypublic final Collection<Namespace> getDeclaredNamespaces()
It is not a "live" representation. Also, it is read-only.
Note: Namespace.equals compares namespace's URI. However, the distinction here is the prefix, because it is mainly for getNamespace(prefix).
public final boolean addDeclaredNamespace(Namespace ns)
DOMException - if the name space with the same prefix
already exists but with different URIpublic final Object getContent()
The content of an element is the first Binary or Text child of the element. Each element can has zero or one content.
Note: getText() returns the concatenation of all Text
children, not just the first one.
getContent(String)public final Object setContent(Object obj)
All existent Binary or Text children of this element are removed first. If the object is a String, a Text item is created to hold it. Otherwise, a Binary item is created to hold it.
Non-Binary/Text children are preserved.
If obj is a Item or an array/collection of Item,
this method will add them as child vertices rather than
being the content.
Moreover, if the first item of the array/collection is Item,
it is assumed to be all valid component to being has valid vertices.
If not, an exception is thrown.
Thus, getContent might not return the object being set by setContent.
obj - the object to set; null is OKgetContent()public final Object getContent(String path)
Note that there might be more than one child with the same path in an iDOM tree; this method simply picks the first one that matches and returns its content. To access certain one, you might use [n] to [@attr = value] specify which one to access.
To know whether the child element exists or content is null,
use hasContent(java.lang.String).
The content of an element is a special feature of iDOM.
Like a Map, it is designed to let developers use names (in a path-like
format) to access objects. See setContent(String, Object).
Like Unix path, the giving name could use '/' to concatenate a series of child elements.
An empty path denotes this element itself. Leading, ending and consecutive '/' will be ignored.
Example:
Object o = element.getContent("abc/def");
String s = Objects.toString(element.getContent("ab/cd"));
element.setContent("t:ab/cd/f:ef", new Integer(10));
TODO: support [n] and [@attr = value]
path - a path; e.g., "b", "a/b", "t:a/t:b"getContent()public final boolean hasContent(String path)
getContent(String).public final Object setContent(String path, Object obj)
Note that there might be more than one child with the same path
in an iDOM tree; this method simply pick one that matches and set
its content (see setContent(Object)).
The content of an element is a special feature of iDOM.
Like a Map, it is designed to let developers use names (in a path-like
format) to access objects. See getContent(String).
Like Unix path, the giving name could use '/' to concatenate a series of child elements.
An empty path denotes this element itself. Leading, ending and consecutive '/' will be ignored.
If any element in the path is not found, it will be created automatically.
path - a path; e.g., "b", "a/b", "t:a/t:b"obj - the object to set; null is acceptablesetContent(Object),
removeContent(java.lang.String),
hasContent(java.lang.String)public final Object removeContent(String path)
Unlike setContent(String, Object) with null,
the child element identified by path will be detached if it has no
other child (but the content). So does its parent
excluding this element. Thus, removeContent(path)
could undo setContent(path, v).
setContent(String, Object)public final void setNamespace(Namespace ns)
Namespace.NO_NAMESPACE.
According W3C/DOM, unlike element, an attribute doesn't allow a namespace that has an URI but without a prefix.
setNamespace in interface Namespaceablepublic final Namespace getNamespace()
NamespaceablegetNamespace in interface Namespaceablepublic final String getTagName()
NamespaceableTo get the local name (the name without prefix), Namespaceable.getLocalName could be used.
getTagName in interface ElementgetTagName in interface Namespaceablepublic final void setTagName(String tname)
NamespaceablesetTagName in interface Namespaceablepublic final String getLocalName()
NamespaceableTo get the tag name (the name with prefix), Namespaceable.getTagName could be used.
getLocalName in interface NodegetLocalName in interface NamespaceablegetLocalName in class AbstractItempublic final void setLocalName(String lname)
NamespaceablesetLocalName in interface Namespaceablepublic final String getName()
getName in interface ItemNamespaceable.getTagName()public final void setName(String tname)
setName in interface ItemsetName in class AbstractItemNamespaceable.setTagName(java.lang.String)public final String getText()
Textual children; never null.
Note: both <tag/> and <tag></tag> returns an
empty string. To tell the difference, check the number of children.getText in interface ItemgetText in class AbstractItemgetText(boolean)public final String getText(boolean trim)
Textual children; never null.trim - whether to trim before returninggetText()public final List<Attribute> getAttributeItems()
AttributableThe returned list is "live". Any modification to it affects the object that owns the attributes.
If the new added attribute has the same tag name as that of any existent attribute, DOMException is thrown. Thus, it is, sometimes, more convenient to ue setAttribute.
Naming reason: we don't call it getAttributes() to avoid the name conflict with Node.getAttributes().
getAttributeItems in interface Attributablepublic final int getAttributeIndex(int indexFrom,
String namespace,
String name,
int mode)
AttributablegetAttributeIndex in interface AttributableindexFrom - the index to start searching from; 0 for beginningnamespace - the namespace URI if FIND_BY_PREFIX is not specified;
the namespace prefix if FIND_BY_PREFIX specified; null to ignorename - the local name if FIND_BY_TAGNAME is not specified;
the tag name if FIND_BY_TAGNAME specified; null to ignoremode - the search mode; zero or any combination of Item.FIND_xxxpublic final int getAttributeIndex(int indexFrom,
String tname)
AttributablegetAttributeIndex in interface AttributableindexFrom - the index to start searching from; 0 for beginningtname - the tag name (i.e., Attribute.getName()) --
consists of the prefix and the local namepublic final Attribute getAttributeItem(String namespace, String name, int mode)
AttributableThe name is a bit strange because we have to avoid name conflicts with org.w3c.dom.Node.
getAttributeItem in interface Attributablenamespace - the namespace URI if FIND_BY_PREFIX is not specified;
the namespace prefix if FIND_BY_PREFIX specified; null to ignorename - the local name if FIND_BY_TAGNAME is not specified;
the tag name if FIND_BY_TAGNAME specified; null to ignoremode - the search mode; zero or any combination of Item.FIND_xxxpublic final Attribute getAttributeItem(String tname)
AttributableThe name is a bit strange because we have to avoid name conflicts with org.w3c.dom.Node.
getAttributeItem in interface Attributabletname - the tag name (i.e., Attribute.getName()) --
consists of the prefix and the local namepublic final List<Attribute> getAttributes(String namespace, String name, int mode)
AttributablegetAttributes in interface Attributablenamespace - the namespace URI if FIND_BY_PREFIX is not specified;
the namespace prefix if FIND_BY_PREFIX specified; null to ignorename - the local name if FIND_BY_TAGNAME is not specified;
the tag name if FIND_BY_TAGNAME specified; null to ignoremode - the search mode; zero or any combination of Item.FIND_xxxpublic final Attribute setAttribute(Attribute attr)
AttributablesetAttribute in interface Attributableattr - the new attribute to addpublic final String getAttributeValue(String namespace, String name, int mode)
AttributableAccording to Section 3.3.3 of XML 1.0 spec, the value is normalized, including trimmed.
getAttributeValue in interface Attributablenamespace - the namespace URI if FIND_BY_PREFIX is not specified;
the namespace prefix if FIND_BY_PREFIX specified; null to ignorename - the local name if FIND_BY_TAGNAME is not specified;
the tag name if FIND_BY_TAGNAME specified; null to ignorepublic final String getAttributeValue(String tname)
AttributableNote: unlike W3C's getAttribute, which returns empty if not specified, this method returns null if not specified.
getAttributeValue in interface Attributablepublic final Attribute setAttributeValue(String tname, String value)
AttributableNote: it looks similar to Attribute(String, String), but this method requires the tag name.
setAttributeValue in interface Attributabletname - the tag name (i.e., Attribute.getName)value - the new value.public Object clone()
AbstractItemclone in interface Itemclone in class AbstractGrouppublic final short getNodeType()
getNodeType in interface Nodepublic final String getNodeValue()
getNodeValue in interface NodegetNodeValue in class AbstractItempublic final NamedNodeMap getAttributes()
getAttributes in interface NodegetAttributes in class AbstractItempublic final boolean hasAttributes()
hasAttributes in interface NodehasAttributes in class AbstractItempublic final String getNamespaceURI()
getNamespaceURI in interface NodegetNamespaceURI in class AbstractItempublic final String getPrefix()
getPrefix in interface NodegetPrefix in class AbstractItempublic final void setPrefix(String prefix)
setPrefix in interface NodesetPrefix in class AbstractItempublic final NodeList getElementsByTagName(String tname)
getElementsByTagName in interface Elementpublic final NodeList getElementsByTagNameNS(String nsURI, String lname)
getElementsByTagNameNS in interface Elementpublic final Attr getAttributeNode(String tname)
getAttributeNode in interface Elementpublic final Attr getAttributeNodeNS(String nsURI, String lname)
getAttributeNodeNS in interface Elementpublic final String getAttribute(String tname)
getAttribute in interface Elementpublic final String getAttributeNS(String nsURI, String lname)
getAttributeNS in interface Elementpublic final void setAttribute(String tname, String value)
setAttribute in interface Elementpublic final void setAttributeNS(String nsURI, String tname, String value)
setAttributeNS in interface Elementpublic final Attr setAttributeNode(Attr newAttr)
setAttributeNode in interface Elementpublic final Attr setAttributeNodeNS(Attr newAttr)
setAttributeNodeNS in interface Elementpublic final void removeAttribute(String tname)
removeAttribute in interface Elementpublic final void removeAttributeNS(String nsURI, String lname)
removeAttributeNS in interface Elementpublic final Attr removeAttributeNode(Attr oldAttr)
removeAttributeNode in interface Elementpublic final boolean hasAttribute(String tname)
hasAttribute in interface Elementpublic final boolean hasAttributeNS(String nsURI, String lname)
hasAttributeNS in interface Elementpublic TypeInfo getSchemaTypeInfo()
getSchemaTypeInfo in interface Elementpublic void setIdAttribute(String name, boolean isId) throws DOMException
setIdAttribute in interface ElementDOMExceptionpublic void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException
setIdAttributeNS in interface ElementDOMExceptionpublic void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException
setIdAttributeNode in interface ElementDOMExceptionCopyright © 2018. All rights reserved.