Package net.n3.nanoxml
Class XMLElement
- java.lang.Object
-
- net.n3.nanoxml.XMLElement
-
- All Implemented Interfaces:
Serializable
,IXMLElement
public class XMLElement extends Object implements IXMLElement, Serializable
XMLElement is an XML element. The standard NanoXML builder generates a tree of such elements.- Version:
- $Name: RELEASE_2_2_1 $, $Revision: 1.5 $
- Author:
- Marc De Scheemaecker
- See Also:
StdXMLBuilder
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
NO_LINE
No line number defined.
-
Constructor Summary
Constructors Constructor Description XMLElement()
Creates an empty element to be used for #PCDATA content.XMLElement(String fullName)
Creates an empty element.XMLElement(String fullName, String namespace)
Creates an empty element.XMLElement(String fullName, String systemID, int lineNr)
Creates an empty element.XMLElement(String fullName, String namespace, String systemID, int lineNr)
Creates an empty element.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addChild(IXMLElement child)
Adds a child element.IXMLElement
createElement(String fullName)
Creates an empty element.IXMLElement
createElement(String fullName, String namespace)
Creates an empty element.IXMLElement
createElement(String fullName, String systemID, int lineNr)
Creates an empty element.IXMLElement
createElement(String fullName, String namespace, String systemID, int lineNr)
Creates an empty element.IXMLElement
createPCDataElement()
Creates an element to be used for #PCDATA content.Enumeration
enumerateAttributeNames()
Returns an enumeration of all attribute names.Enumeration
enumerateChildren()
Returns an enumeration of all child elements.boolean
equals(Object rawElement)
Returns true if the element equals another element.boolean
equalsXMLElement(IXMLElement elt)
Returns true if the element equals another element.protected void
finalize()
Cleans up the object when it's destroyed.String
getAttribute(String name)
Deprecated.As of NanoXML/Java 2.1, replaced bygetAttribute(java.lang.String,java.lang.String)
Returns the value of an attribute.int
getAttribute(String name, int defaultValue)
Returns the value of an attribute.String
getAttribute(String name, String defaultValue)
Returns the value of an attribute.int
getAttribute(String name, String namespace, int defaultValue)
Returns the value of an attribute.String
getAttribute(String name, String namespace, String defaultValue)
Returns the value of an attribute.int
getAttributeCount()
Returns the number of attributes.String
getAttributeNamespace(String name)
Returns the namespace of an attribute.Properties
getAttributes()
Returns all attributes as a Properties object.Properties
getAttributesInNamespace(String namespace)
Returns all attributes in a specific namespace as a Properties object.String
getAttributeType(String name)
Returns the type of an attribute.String
getAttributeType(String name, String namespace)
Returns the type of an attribute.IXMLElement
getChildAtIndex(int index)
Returns the child at a specific index.Vector
getChildren()
Returns a vector containing all the child elements.int
getChildrenCount()
Returns the number of children.Vector
getChildrenNamed(String name)
Returns a vector of all child elements named name.Vector
getChildrenNamed(String name, String namespace)
Returns a vector of all child elements named name.String
getContent()
Return the #PCDATA content of the element.IXMLElement
getFirstChildNamed(String name)
Searches a child element.IXMLElement
getFirstChildNamed(String name, String namespace)
Searches a child element.String
getFullName()
Returns the full name (i.e.int
getLineNr()
Returns the line number in the data where the element started.String
getName()
Returns the name of the element.String
getNamespace()
Returns the namespace of the element.IXMLElement
getParent()
Returns the parent element.String
getSystemID()
Returns the system ID of the data where the element started.boolean
hasAttribute(String name)
Returns whether an attribute exists.boolean
hasAttribute(String name, String namespace)
Returns whether an attribute exists.boolean
hasChildren()
Returns whether the element has children.void
insertChild(IXMLElement child, int index)
Inserts a child element.boolean
isLeaf()
Returns whether the element is a leaf element.void
removeAttribute(String name)
Removes an attribute.void
removeAttribute(String name, String namespace)
Removes an attribute.void
removeChild(IXMLElement child)
Removes a child element.void
removeChildAtIndex(int index)
Removes the child located at a certain index.void
setAttribute(String name, String value)
Sets an attribute.void
setAttribute(String fullName, String namespace, String value)
Sets an attribute.void
setContent(String content)
Sets the #PCDATA content.void
setName(String name)
Sets the full name.void
setName(String fullName, String namespace)
Sets the name.
-
-
-
Field Detail
-
NO_LINE
public static final int NO_LINE
No line number defined.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
XMLElement
public XMLElement()
Creates an empty element to be used for #PCDATA content.
-
XMLElement
public XMLElement(String fullName)
Creates an empty element.- Parameters:
fullName
- the name of the element.
-
XMLElement
public XMLElement(String fullName, String systemID, int lineNr)
Creates an empty element.- Parameters:
fullName
- the name of the element.systemID
- the system ID of the XML data where the element starts.lineNr
- the line in the XML data where the element starts.
-
XMLElement
public XMLElement(String fullName, String namespace)
Creates an empty element.- Parameters:
fullName
- the full name of the elementnamespace
- the namespace URI.
-
XMLElement
public XMLElement(String fullName, String namespace, String systemID, int lineNr)
Creates an empty element.- Parameters:
fullName
- the full name of the elementnamespace
- the namespace URI.systemID
- the system ID of the XML data where the element starts.lineNr
- the line in the XML data where the element starts.
-
-
Method Detail
-
createPCDataElement
public IXMLElement createPCDataElement()
Creates an element to be used for #PCDATA content.- Specified by:
createPCDataElement
in interfaceIXMLElement
-
createElement
public IXMLElement createElement(String fullName)
Creates an empty element.- Specified by:
createElement
in interfaceIXMLElement
- Parameters:
fullName
- the name of the element.
-
createElement
public IXMLElement createElement(String fullName, String systemID, int lineNr)
Creates an empty element.- Specified by:
createElement
in interfaceIXMLElement
- Parameters:
fullName
- the name of the element.systemID
- the system ID of the XML data where the element starts.lineNr
- the line in the XML data where the element starts.
-
createElement
public IXMLElement createElement(String fullName, String namespace)
Creates an empty element.- Specified by:
createElement
in interfaceIXMLElement
- Parameters:
fullName
- the full name of the elementnamespace
- the namespace URI.
-
createElement
public IXMLElement createElement(String fullName, String namespace, String systemID, int lineNr)
Creates an empty element.- Specified by:
createElement
in interfaceIXMLElement
- Parameters:
fullName
- the full name of the elementnamespace
- the namespace URI.systemID
- the system ID of the XML data where the element starts.lineNr
- the line in the XML data where the element starts.
-
finalize
protected void finalize() throws Throwable
Cleans up the object when it's destroyed.
-
getParent
public IXMLElement getParent()
Returns the parent element. This method returns null for the root element.- Specified by:
getParent
in interfaceIXMLElement
-
getFullName
public String getFullName()
Returns the full name (i.e. the name including an eventual namespace prefix) of the element.- Specified by:
getFullName
in interfaceIXMLElement
- Returns:
- the name, or null if the element only contains #PCDATA.
-
getName
public String getName()
Returns the name of the element.- Specified by:
getName
in interfaceIXMLElement
- Returns:
- the name, or null if the element only contains #PCDATA.
-
getNamespace
public String getNamespace()
Returns the namespace of the element.- Specified by:
getNamespace
in interfaceIXMLElement
- Returns:
- the namespace, or null if no namespace is associated with the element.
-
setName
public void setName(String name)
Sets the full name. This method also sets the short name and clears the namespace URI.- Specified by:
setName
in interfaceIXMLElement
- Parameters:
name
- the non-null name.
-
setName
public void setName(String fullName, String namespace)
Sets the name.- Specified by:
setName
in interfaceIXMLElement
- Parameters:
fullName
- the non-null full name.namespace
- the namespace URI, which may be null.
-
addChild
public void addChild(IXMLElement child)
Adds a child element.- Specified by:
addChild
in interfaceIXMLElement
- Parameters:
child
- the non-null child to add.
-
insertChild
public void insertChild(IXMLElement child, int index)
Inserts a child element.- Parameters:
child
- the non-null child to add.index
- where to put the child.
-
removeChild
public void removeChild(IXMLElement child)
Removes a child element.- Specified by:
removeChild
in interfaceIXMLElement
- Parameters:
child
- the non-null child to remove.
-
removeChildAtIndex
public void removeChildAtIndex(int index)
Removes the child located at a certain index.- Specified by:
removeChildAtIndex
in interfaceIXMLElement
- Parameters:
index
- the index of the child, where the first child has index 0.
-
enumerateChildren
public Enumeration enumerateChildren()
Returns an enumeration of all child elements.- Specified by:
enumerateChildren
in interfaceIXMLElement
- Returns:
- the non-null enumeration
-
isLeaf
public boolean isLeaf()
Returns whether the element is a leaf element.- Specified by:
isLeaf
in interfaceIXMLElement
- Returns:
- true if the element has no children.
-
hasChildren
public boolean hasChildren()
Returns whether the element has children.- Specified by:
hasChildren
in interfaceIXMLElement
- Returns:
- true if the element has children.
-
getChildrenCount
public int getChildrenCount()
Returns the number of children.- Specified by:
getChildrenCount
in interfaceIXMLElement
- Returns:
- the count.
-
getChildren
public Vector getChildren()
Returns a vector containing all the child elements.- Specified by:
getChildren
in interfaceIXMLElement
- Returns:
- the vector.
-
getChildAtIndex
public IXMLElement getChildAtIndex(int index) throws ArrayIndexOutOfBoundsException
Returns the child at a specific index.- Specified by:
getChildAtIndex
in interfaceIXMLElement
- Parameters:
index
- the index of the child- Returns:
- the non-null child
- Throws:
ArrayIndexOutOfBoundsException
- if the index is out of bounds.
-
getFirstChildNamed
public IXMLElement getFirstChildNamed(String name)
Searches a child element.- Specified by:
getFirstChildNamed
in interfaceIXMLElement
- Parameters:
name
- the full name of the child to search for.- Returns:
- the child element, or null if no such child was found.
-
getFirstChildNamed
public IXMLElement getFirstChildNamed(String name, String namespace)
Searches a child element.- Specified by:
getFirstChildNamed
in interfaceIXMLElement
- Parameters:
name
- the name of the child to search for.namespace
- the namespace, which may be null.- Returns:
- the child element, or null if no such child was found.
-
getChildrenNamed
public Vector getChildrenNamed(String name)
Returns a vector of all child elements named name.- Specified by:
getChildrenNamed
in interfaceIXMLElement
- Parameters:
name
- the full name of the children to search for.- Returns:
- the non-null vector of child elements.
-
getChildrenNamed
public Vector getChildrenNamed(String name, String namespace)
Returns a vector of all child elements named name.- Specified by:
getChildrenNamed
in interfaceIXMLElement
- Parameters:
name
- the name of the children to search for.namespace
- the namespace, which may be null.- Returns:
- the non-null vector of child elements.
-
getAttributeCount
public int getAttributeCount()
Returns the number of attributes.- Specified by:
getAttributeCount
in interfaceIXMLElement
-
getAttribute
public String getAttribute(String name)
Deprecated.As of NanoXML/Java 2.1, replaced bygetAttribute(java.lang.String,java.lang.String)
Returns the value of an attribute.- Specified by:
getAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.- Returns:
- the value, or null if the attribute does not exist.
-
getAttribute
public String getAttribute(String name, String defaultValue)
Returns the value of an attribute.- Specified by:
getAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null full name of the attribute.defaultValue
- the default value of the attribute.- Returns:
- the value, or defaultValue if the attribute does not exist.
-
getAttribute
public String getAttribute(String name, String namespace, String defaultValue)
Returns the value of an attribute.- Specified by:
getAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.namespace
- the namespace URI, which may be null.defaultValue
- the default value of the attribute.- Returns:
- the value, or defaultValue if the attribute does not exist.
-
getAttribute
public int getAttribute(String name, int defaultValue)
Returns the value of an attribute.- Specified by:
getAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null full name of the attribute.defaultValue
- the default value of the attribute.- Returns:
- the value, or defaultValue if the attribute does not exist.
-
getAttribute
public int getAttribute(String name, String namespace, int defaultValue)
Returns the value of an attribute.- Specified by:
getAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.namespace
- the namespace URI, which may be null.defaultValue
- the default value of the attribute.- Returns:
- the value, or defaultValue if the attribute does not exist.
-
getAttributeType
public String getAttributeType(String name)
Returns the type of an attribute.- Specified by:
getAttributeType
in interfaceIXMLElement
- Parameters:
name
- the non-null full name of the attribute.- Returns:
- the type, or null if the attribute does not exist.
-
getAttributeNamespace
public String getAttributeNamespace(String name)
Returns the namespace of an attribute.- Specified by:
getAttributeNamespace
in interfaceIXMLElement
- Parameters:
name
- the non-null full name of the attribute.- Returns:
- the namespace, or null if there is none associated.
-
getAttributeType
public String getAttributeType(String name, String namespace)
Returns the type of an attribute.- Specified by:
getAttributeType
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.namespace
- the namespace URI, which may be null.- Returns:
- the type, or null if the attribute does not exist.
-
setAttribute
public void setAttribute(String name, String value)
Sets an attribute.- Specified by:
setAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null full name of the attribute.value
- the non-null value of the attribute.
-
setAttribute
public void setAttribute(String fullName, String namespace, String value)
Sets an attribute.- Specified by:
setAttribute
in interfaceIXMLElement
- Parameters:
fullName
- the non-null full name of the attribute.namespace
- the namespace URI of the attribute, which may be null.value
- the non-null value of the attribute.
-
removeAttribute
public void removeAttribute(String name)
Removes an attribute.- Specified by:
removeAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.
-
removeAttribute
public void removeAttribute(String name, String namespace)
Removes an attribute.- Specified by:
removeAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.namespace
- the namespace URI of the attribute, which may be null.
-
enumerateAttributeNames
public Enumeration enumerateAttributeNames()
Returns an enumeration of all attribute names.- Specified by:
enumerateAttributeNames
in interfaceIXMLElement
- Returns:
- the non-null enumeration.
-
hasAttribute
public boolean hasAttribute(String name)
Returns whether an attribute exists.- Specified by:
hasAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.- Returns:
- true if the attribute exists.
-
hasAttribute
public boolean hasAttribute(String name, String namespace)
Returns whether an attribute exists.- Specified by:
hasAttribute
in interfaceIXMLElement
- Parameters:
name
- the non-null name of the attribute.namespace
- the namespace URI of the attribute, which may be null.- Returns:
- true if the attribute exists.
-
getAttributes
public Properties getAttributes()
Returns all attributes as a Properties object.- Specified by:
getAttributes
in interfaceIXMLElement
- Returns:
- the non-null set.
-
getAttributesInNamespace
public Properties getAttributesInNamespace(String namespace)
Returns all attributes in a specific namespace as a Properties object.- Specified by:
getAttributesInNamespace
in interfaceIXMLElement
- Parameters:
namespace
- the namespace URI of the attributes, which may be null.- Returns:
- the non-null set.
-
getSystemID
public String getSystemID()
Returns the system ID of the data where the element started.- Specified by:
getSystemID
in interfaceIXMLElement
- Returns:
- the system ID, or null if unknown.
- See Also:
getLineNr()
-
getLineNr
public int getLineNr()
Returns the line number in the data where the element started.- Specified by:
getLineNr
in interfaceIXMLElement
- Returns:
- the line number, or NO_LINE if unknown.
- See Also:
NO_LINE
,getSystemID()
-
getContent
public String getContent()
Return the #PCDATA content of the element. If the element has a combination of #PCDATA content and child elements, the #PCDATA sections can be retrieved as unnamed child objects. In this case, this method returns null.- Specified by:
getContent
in interfaceIXMLElement
- Returns:
- the content.
-
setContent
public void setContent(String content)
Sets the #PCDATA content. It is an error to call this method with a non-null value if there are child objects.- Specified by:
setContent
in interfaceIXMLElement
- Parameters:
content
- the (possibly null) content.
-
equals
public boolean equals(Object rawElement)
Returns true if the element equals another element.- Specified by:
equals
in interfaceIXMLElement
- Overrides:
equals
in classObject
- Parameters:
rawElement
- the element to compare to
-
equalsXMLElement
public boolean equalsXMLElement(IXMLElement elt)
Returns true if the element equals another element.- Specified by:
equalsXMLElement
in interfaceIXMLElement
- Parameters:
rawElement
- the element to compare to
-
-