processing.xml
Class XMLElement

java.lang.Object
  extended by processing.xml.XMLElement
All Implemented Interfaces:
java.io.Serializable

public class XMLElement
extends java.lang.Object
implements java.io.Serializable

XMLElement is an XML element. This is the base class used for the Processing XML library, representing a single node of an XML tree. This code is based on a modified version of NanoXML by Marc De Scheemaecker.

See Also:
Serialized Form

Field Summary
static int NO_LINE
          No line number defined.
 
Constructor Summary
XMLElement()
          Creates an empty element to be used for #PCDATA content.
XMLElement(PApplet parent, java.lang.String filename)
          Begin parsing XML data passed in from a PApplet.
XMLElement(java.io.Reader r)
           
XMLElement(java.lang.String xml)
           
XMLElement(java.lang.String fullName, java.lang.String namespace, java.lang.String systemID, int lineNr)
          Creates an empty element.
 
Method Summary
 void addChild(XMLElement child)
          Adds a child element.
 XMLElement createElement(java.lang.String fullName, java.lang.String namespace)
          Creates an empty element.
 XMLElement createElement(java.lang.String fullName, java.lang.String namespace, java.lang.String systemID, int lineNr)
          Creates an empty element.
 XMLElement createPCDataElement()
          Creates an element to be used for #PCDATA content.
 java.util.Enumeration<java.lang.String> enumerateAttributeNames()
          Returns an enumeration of all attribute names.
 java.util.Enumeration<XMLElement> enumerateChildren()
          Returns an enumeration of all child elements.
 boolean equals(java.lang.Object rawElement)
          Returns true if the element equals another element.
 boolean equalsXMLElement(XMLElement rawElement)
          Returns true if the element equals another element.
 java.lang.String getAttribute(java.lang.String name)
          Returns the value of an attribute.
 java.lang.String getAttribute(java.lang.String name, java.lang.String defaultValue)
          Returns the value of an attribute.
 java.lang.String getAttribute(java.lang.String name, java.lang.String namespace, java.lang.String defaultValue)
          Returns the value of an attribute.
 int getAttributeCount()
          Returns the number of attributes.
 java.lang.String getAttributeNamespace(java.lang.String name)
          Returns the namespace of an attribute.
 java.util.Properties getAttributes()
          Returns all attributes as a Properties object.
 java.util.Properties getAttributesInNamespace(java.lang.String namespace)
          Returns all attributes in a specific namespace as a Properties object.
 java.lang.String getAttributeType(java.lang.String name)
          Returns the type of an attribute.
 java.lang.String getAttributeType(java.lang.String name, java.lang.String namespace)
          Returns the type of an attribute.
 XMLElement getChild(int which)
          Quick accessor for an element at a particular index.
 XMLElement getChild(java.lang.String name)
          Get a child by its name or path.
 XMLElement getChildAtIndex(int index)
          Returns the child at a specific index.
 int getChildCount()
          Returns the number of children.
 XMLElement[] getChildren()
          Returns an array containing all the child elements.
 XMLElement[] getChildren(java.lang.String name)
          Get any children that match this name or path.
 java.lang.String getContent()
          Return the #PCDATA content of the element.
 double getDoubleAttribute(java.lang.String name)
           
 double getDoubleAttribute(java.lang.String name, double defaultValue)
          Returns the value of an attribute.
 double getDoubleAttribute(java.lang.String name, java.lang.String namespace, double defaultValue)
          Returns the value of an attribute.
 float getFloatAttribute(java.lang.String name)
           
 float getFloatAttribute(java.lang.String name, float defaultValue)
          Returns the value of an attribute.
 float getFloatAttribute(java.lang.String name, java.lang.String namespace, float defaultValue)
          Returns the value of an attribute.
 int getIntAttribute(java.lang.String name)
           
 int getIntAttribute(java.lang.String name, int defaultValue)
          Returns the value of an attribute.
 int getIntAttribute(java.lang.String name, java.lang.String namespace, int defaultValue)
          Returns the value of an attribute.
 int getLineNr()
          Returns the line number in the data where the element started.
 java.lang.String getLocalName()
          Returns the name of the element.
 java.lang.String getName()
          Returns the full name (i.e.
 java.lang.String getNamespace()
          Returns the namespace of the element.
 XMLElement getParent()
          Returns the parent element.
 java.lang.String getStringAttribute(java.lang.String name)
           
 java.lang.String getStringAttribute(java.lang.String name, java.lang.String defaultValue)
           
 java.lang.String getStringAttribute(java.lang.String name, java.lang.String namespace, java.lang.String defaultValue)
           
 java.lang.String getSystemID()
          Returns the system ID of the data where the element started.
 boolean hasAttribute(java.lang.String name)
          Returns whether an attribute exists.
 boolean hasAttribute(java.lang.String name, java.lang.String namespace)
          Returns whether an attribute exists.
 boolean hasChildren()
          Returns whether the element has children.
 void insertChild(XMLElement child, int index)
          Inserts a child element.
 boolean isLeaf()
          Returns whether the element is a leaf element.
 java.lang.String[] listChildren()
          Put the names of all children into an array.
 void removeAttribute(java.lang.String name)
          Removes an attribute.
 void removeAttribute(java.lang.String name, java.lang.String namespace)
          Removes an attribute.
 void removeChild(XMLElement child)
          Removes a child element.
 void removeChildAtIndex(int index)
          Removes the child located at a certain index.
 void setAttribute(java.lang.String name, java.lang.String value)
          Sets an attribute.
 void setAttribute(java.lang.String fullName, java.lang.String namespace, java.lang.String value)
          Sets an attribute.
 void setContent(java.lang.String content)
          Sets the #PCDATA content.
 void setName(java.lang.String name)
          Sets the full name.
 void setName(java.lang.String fullName, java.lang.String namespace)
          Sets the name.
 java.lang.String toString(boolean pretty)
           
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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(java.lang.String fullName,
                  java.lang.String namespace,
                  java.lang.String systemID,
                  int lineNr)
Creates an empty element.

Parameters:
fullName - the full name of the element
namespace - 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.

XMLElement

public XMLElement(PApplet parent,
                  java.lang.String filename)
Begin parsing XML data passed in from a PApplet. This code wraps exception handling, for more advanced exception handling, use the constructor that takes a Reader or InputStream.

Parameters:
filename -
parent -

XMLElement

public XMLElement(java.io.Reader r)

XMLElement

public XMLElement(java.lang.String xml)
Method Detail

createPCDataElement

public XMLElement createPCDataElement()
Creates an element to be used for #PCDATA content.


createElement

public XMLElement createElement(java.lang.String fullName,
                                java.lang.String namespace)
Creates an empty element.

Parameters:
fullName - the full name of the element
namespace - the namespace URI.

createElement

public XMLElement createElement(java.lang.String fullName,
                                java.lang.String namespace,
                                java.lang.String systemID,
                                int lineNr)
Creates an empty element.

Parameters:
fullName - the full name of the element
namespace - 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.

getParent

public XMLElement getParent()
Returns the parent element. This method returns null for the root element.


getName

public java.lang.String getName()
Returns the full name (i.e. the name including an eventual namespace prefix) of the element.

Returns:
the name, or null if the element only contains #PCDATA.

getLocalName

public java.lang.String getLocalName()
Returns the name of the element.

Returns:
the name, or null if the element only contains #PCDATA.

getNamespace

public java.lang.String getNamespace()
Returns the namespace of the element.

Returns:
the namespace, or null if no namespace is associated with the element.

setName

public void setName(java.lang.String name)
Sets the full name. This method also sets the short name and clears the namespace URI.

Parameters:
name - the non-null name.

setName

public void setName(java.lang.String fullName,
                    java.lang.String namespace)
Sets the name.

Parameters:
fullName - the non-null full name.
namespace - the namespace URI, which may be null.

addChild

public void addChild(XMLElement child)
Adds a child element.

Parameters:
child - the non-null child to add.

insertChild

public void insertChild(XMLElement 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(XMLElement child)
Removes a child element.

Parameters:
child - the non-null child to remove.

removeChildAtIndex

public void removeChildAtIndex(int index)
Removes the child located at a certain index.

Parameters:
index - the index of the child, where the first child has index 0.

enumerateChildren

public java.util.Enumeration<XMLElement> enumerateChildren()
Returns an enumeration of all child elements.

Returns:
the non-null enumeration

isLeaf

public boolean isLeaf()
Returns whether the element is a leaf element.

Returns:
true if the element has no children.

hasChildren

public boolean hasChildren()
Returns whether the element has children.

Returns:
true if the element has children.

getChildCount

public int getChildCount()
Returns the number of children.

Returns:
the count.

listChildren

public java.lang.String[] listChildren()
Put the names of all children into an array. Same as looping through each child and calling getName() on each XMLElement.


getChildren

public XMLElement[] getChildren()
Returns an array containing all the child elements.


getChild

public XMLElement getChild(int which)
Quick accessor for an element at a particular index.


getChild

public XMLElement getChild(java.lang.String name)
Get a child by its name or path.

Parameters:
name - element name or path/to/element
Returns:
the element

getChildAtIndex

public XMLElement getChildAtIndex(int index)
                           throws java.lang.ArrayIndexOutOfBoundsException
Returns the child at a specific index.

Parameters:
index - the index of the child
Returns:
the non-null child
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is out of bounds.

getChildren

public XMLElement[] getChildren(java.lang.String name)
Get any children that match this name or path. Similar to getChild(), but will grab multiple matches rather than only the first.

Parameters:
name - element name or path/to/element
Returns:
array of child elements that match

getAttributeCount

public int getAttributeCount()
Returns the number of attributes.


getAttribute

public java.lang.String getAttribute(java.lang.String name)
Returns the value of an attribute.

Parameters:
name - the non-null name of the attribute.
Returns:
the value, or null if the attribute does not exist.

getAttribute

public java.lang.String getAttribute(java.lang.String name,
                                     java.lang.String defaultValue)
Returns the value of an attribute.

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 java.lang.String getAttribute(java.lang.String name,
                                     java.lang.String namespace,
                                     java.lang.String defaultValue)
Returns the value of an attribute.

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.

getStringAttribute

public java.lang.String getStringAttribute(java.lang.String name)

getStringAttribute

public java.lang.String getStringAttribute(java.lang.String name,
                                           java.lang.String defaultValue)

getStringAttribute

public java.lang.String getStringAttribute(java.lang.String name,
                                           java.lang.String namespace,
                                           java.lang.String defaultValue)

getIntAttribute

public int getIntAttribute(java.lang.String name)

getIntAttribute

public int getIntAttribute(java.lang.String name,
                           int defaultValue)
Returns the value of an attribute.

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.

getIntAttribute

public int getIntAttribute(java.lang.String name,
                           java.lang.String namespace,
                           int defaultValue)
Returns the value of an attribute.

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.

getFloatAttribute

public float getFloatAttribute(java.lang.String name)

getFloatAttribute

public float getFloatAttribute(java.lang.String name,
                               float defaultValue)
Returns the value of an attribute.

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.

getFloatAttribute

public float getFloatAttribute(java.lang.String name,
                               java.lang.String namespace,
                               float defaultValue)
Returns the value of an attribute.

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.

getDoubleAttribute

public double getDoubleAttribute(java.lang.String name)

getDoubleAttribute

public double getDoubleAttribute(java.lang.String name,
                                 double defaultValue)
Returns the value of an attribute.

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.

getDoubleAttribute

public double getDoubleAttribute(java.lang.String name,
                                 java.lang.String namespace,
                                 double defaultValue)
Returns the value of an attribute.

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 java.lang.String getAttributeType(java.lang.String name)
Returns the type of an attribute.

Parameters:
name - the non-null full name of the attribute.
Returns:
the type, or null if the attribute does not exist.

getAttributeNamespace

public java.lang.String getAttributeNamespace(java.lang.String name)
Returns the namespace of an attribute.

Parameters:
name - the non-null full name of the attribute.
Returns:
the namespace, or null if there is none associated.

getAttributeType

public java.lang.String getAttributeType(java.lang.String name,
                                         java.lang.String namespace)
Returns the type of an attribute.

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(java.lang.String name,
                         java.lang.String value)
Sets an attribute.

Parameters:
name - the non-null full name of the attribute.
value - the non-null value of the attribute.

setAttribute

public void setAttribute(java.lang.String fullName,
                         java.lang.String namespace,
                         java.lang.String value)
Sets an attribute.

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(java.lang.String name)
Removes an attribute.

Parameters:
name - the non-null name of the attribute.

removeAttribute

public void removeAttribute(java.lang.String name,
                            java.lang.String namespace)
Removes an attribute.

Parameters:
name - the non-null name of the attribute.
namespace - the namespace URI of the attribute, which may be null.

enumerateAttributeNames

public java.util.Enumeration<java.lang.String> enumerateAttributeNames()
Returns an enumeration of all attribute names.

Returns:
the non-null enumeration.

hasAttribute

public boolean hasAttribute(java.lang.String name)
Returns whether an attribute exists.

Returns:
true if the attribute exists.

hasAttribute

public boolean hasAttribute(java.lang.String name,
                            java.lang.String namespace)
Returns whether an attribute exists.

Returns:
true if the attribute exists.

getAttributes

public java.util.Properties getAttributes()
Returns all attributes as a Properties object.

Returns:
the non-null set.

getAttributesInNamespace

public java.util.Properties getAttributesInNamespace(java.lang.String namespace)
Returns all attributes in a specific namespace as a Properties object.

Parameters:
namespace - the namespace URI of the attributes, which may be null.
Returns:
the non-null set.

getSystemID

public java.lang.String getSystemID()
Returns the system ID of the data where the element started.

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.

Returns:
the line number, or NO_LINE if unknown.
See Also:
NO_LINE, getSystemID()

getContent

public java.lang.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.

Returns:
the content.

setContent

public void setContent(java.lang.String content)
Sets the #PCDATA content. It is an error to call this method with a non-null value if there are child objects.

Parameters:
content - the (possibly null) content.

equals

public boolean equals(java.lang.Object rawElement)
Returns true if the element equals another element.

Overrides:
equals in class java.lang.Object
Parameters:
rawElement - the element to compare to

equalsXMLElement

public boolean equalsXMLElement(XMLElement rawElement)
Returns true if the element equals another element.

Parameters:
rawElement - the element to compare to

toString

public java.lang.String toString(boolean pretty)