- java.lang.Object
-
- processing.data.XML
-
- All Implemented Interfaces:
Serializable
public class XML extends Object implements Serializable
This is the base class used for the Processing XML library, representing a single node of an XML tree.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedXML()XML(File file)Advanced users only; use loadXML() in PApplet.XML(File file, String options)Advanced users only; use loadXML() in PApplet.XML(InputStream input)XML(InputStream input, String options)Unlike the loadXML() method in PApplet, this version works with files that are not in UTF-8 format.XML(Reader reader)Advanced users only; use loadXML() in PApplet.XML(Reader reader, String options)Advanced users only; use loadXML() in PApplet.XML(String name)protectedXML(XML parent, Node node)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description XMLaddChild(String tag)XMLaddChild(XML child)protected XMLappendChild(Node newNode)Internal handler to add the node structure.protected voidcheckChildren()Honey, can you just check on the kids? Thanks.Stringformat(int indent)Format this XML data as a String.intgetAttributeCount()Returns the number of attributes.XMLgetChild(int index)Quick accessor for an element at a particular index.XMLgetChild(String name)Get a child by its name or path.intgetChildCount()Returns the number of children.protected XMLgetChildRecursive(String[] items, int offset)Internal helper function for getChild(String).XML[]getChildren()Returns an array containing all the child elements.XML[]getChildren(String name)Get any children that match this name or path.protected XML[]getChildrenRecursive(String[] items, int offset)StringgetContent()Return the #PCDATA content of the element.StringgetContent(String defaultValue)doublegetDouble(String name)doublegetDouble(String name, double defaultValue)Returns the value of an attribute.doublegetDoubleContent()doublegetDoubleContent(double defaultValue)floatgetFloat(String name)Returns the value of an attribute, or zero if not present.floatgetFloat(String name, float defaultValue)Returns the value of an attribute.floatgetFloatContent()floatgetFloatContent(float defaultValue)intgetInt(String name)intgetInt(String name, int defaultValue)Returns the value of an attribute.intgetIntContent()intgetIntContent(int defaultValue)StringgetLocalName()Returns the name of the element (without namespace prefix).longgetLong(String name, long defaultValue)Returns the value of an attribute.longgetLongContent()longgetLongContent(long defaultValue)StringgetName()Returns the full name (i.e.protected ObjectgetNative()Internal function; not included in reference.XMLgetParent()Returns the parent element.StringgetString(String name)StringgetString(String name, String defaultValue)booleanhasAttribute(String name)Returns whether an attribute exists.booleanhasChildren()Returns a boolean of whether or not there are children.String[]listAttributes()Get a list of the names for all of the attributes for this node.String[]listChildren()Put the names of all children into an array.static XMLparse(String data)static XMLparse(String data, String options)voidprint()voidremoveChild(XML kid)booleansave(File file)booleansave(File file, String options)voidsetContent(String text)voidsetDouble(String name, double value)voidsetDoubleContent(double value)voidsetFloat(String name, float value)voidsetFloatContent(float value)voidsetInt(String name, int value)voidsetIntContent(int value)voidsetLong(String name, long value)voidsetLongContent(long value)voidsetName(String newName)voidsetString(String name, String value)StringtoString()Return the XML document formatted with two spaces for indents.voidtrim()Removes whitespace nodes.booleanwrite(PrintWriter output)
-
-
-
Constructor Detail
-
XML
protected XML()
-
XML
public XML(File file) throws IOException, ParserConfigurationException, SAXException
Advanced users only; use loadXML() in PApplet. This is not a supported function and is subject to change. It is available simply for users that would like to handle the exceptions in a particular way.
-
XML
public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException
Advanced users only; use loadXML() in PApplet.
-
XML
public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException
-
XML
public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException
Unlike the loadXML() method in PApplet, this version works with files that are not in UTF-8 format.
-
XML
public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException
Advanced users only; use loadXML() in PApplet.
-
XML
public XML(Reader reader, String options) throws IOException, ParserConfigurationException, SAXException
Advanced users only; use loadXML() in PApplet. Added extra code to handle (Unicode NLF), which is sometimes inserted by web browsers (Safari?) and not distinguishable from a "real" LF (or CRLF) in some text editors (i.e. TextEdit on OS X). Only doing this for XML (and not all Reader objects) because LFs are essential. https://github.com/processing/processing/issues/2100
-
XML
public XML(String name)
- Parameters:
name- creates a node with this name
-
-
Method Detail
-
parse
public static XML parse(String data) throws IOException, ParserConfigurationException, SAXException
- Parameters:
data- the content to be parsed as XML- Returns:
- an XML object, or null
- Throws:
SAXExceptionParserConfigurationExceptionIOException
-
parse
public static XML parse(String data, String options) throws IOException, ParserConfigurationException, SAXException
-
save
public boolean save(File file)
-
write
public boolean write(PrintWriter output)
-
getParent
public XML getParent()
Returns the parent element. This method returns null for the root element.
-
getNative
protected Object getNative()
Internal function; not included in reference.
-
getName
public 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.
-
setName
public void setName(String newName)
-
getLocalName
public String getLocalName()
Returns the name of the element (without namespace prefix). Internal function; not included in reference.
-
checkChildren
protected void checkChildren()
Honey, can you just check on the kids? Thanks. Internal function; not included in reference.
-
getChildCount
public int getChildCount()
Returns the number of children.- Returns:
- the count.
-
hasChildren
public boolean hasChildren()
Returns a boolean of whether or not there are children.
-
listChildren
public 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 XML[] getChildren()
Returns an array containing all the child elements.
-
getChild
public XML getChild(int index)
Quick accessor for an element at a particular index.
-
getChild
public XML getChild(String name)
Get a child by its name or path.- Parameters:
name- element name or path/to/element- Returns:
- the first matching element or null if no match
-
getChildRecursive
protected XML getChildRecursive(String[] items, int offset)
Internal helper function for getChild(String).- Parameters:
items- result of splitting the query on slashesoffset- where in the items[] array we're currently looking- Returns:
- matching element or null if no match
-
getChildren
public XML[] getChildren(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
-
removeChild
public void removeChild(XML kid)
-
trim
public void trim()
Removes whitespace nodes. Those whitespace nodes are required to reconstruct the original XML's spacing and indentation. If you call this and use saveXML() your original spacing will be gone.
-
getAttributeCount
public int getAttributeCount()
Returns the number of attributes.
-
listAttributes
public String[] listAttributes()
Get a list of the names for all of the attributes for this node.
-
hasAttribute
public boolean hasAttribute(String name)
Returns whether an attribute exists.
-
getInt
public int getInt(String name)
-
setInt
public void setInt(String name, int value)
-
getInt
public int getInt(String name, int defaultValue)
Returns the value of an attribute.- Parameters:
name- the non-null full name of the attributedefaultValue- the default value of the attribute- Returns:
- the value, or defaultValue if the attribute does not exist
-
setLong
public void setLong(String name, long value)
-
getLong
public long getLong(String name, long 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.
-
getFloat
public float getFloat(String name)
Returns the value of an attribute, or zero if not present.
-
getFloat
public float getFloat(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.
-
setFloat
public void setFloat(String name, float value)
-
getDouble
public double getDouble(String name)
-
getDouble
public double getDouble(String name, double defaultValue)
Returns the value of an attribute.- Parameters:
name- the non-null full name of the attributedefaultValue- the default value of the attribute- Returns:
- the value, or defaultValue if the attribute does not exist
-
setDouble
public void setDouble(String name, double value)
-
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.- Returns:
- the content.
- See Also:
getIntContent(),getFloatContent()
-
getIntContent
public int getIntContent()
- Returns:
- the content.
- See Also:
getContent(),getFloatContent()
-
getIntContent
public int getIntContent(int defaultValue)
- Parameters:
defaultValue- the default value of the attribute
-
getFloatContent
public float getFloatContent()
- Returns:
- the content.
- See Also:
getContent(),getIntContent()
-
getFloatContent
public float getFloatContent(float defaultValue)
- Parameters:
defaultValue- the default value of the attribute
-
getLongContent
public long getLongContent()
-
getLongContent
public long getLongContent(long defaultValue)
-
getDoubleContent
public double getDoubleContent()
-
getDoubleContent
public double getDoubleContent(double defaultValue)
-
setContent
public void setContent(String text)
-
setIntContent
public void setIntContent(int value)
-
setFloatContent
public void setFloatContent(float value)
-
setLongContent
public void setLongContent(long value)
-
setDoubleContent
public void setDoubleContent(double value)
-
format
public String format(int indent)
Format this XML data as a String.- Parameters:
indent- -1 for a single line (and no declaration), >= 0 for indents and newlines- Returns:
- the content
- See Also:
toString()
-
print
public void print()
-
toString
public String toString()
Return the XML document formatted with two spaces for indents. Chosen to do this since it's the most common case (e.g. with println()). Same as format(2). Use the format() function for more options.- Overrides:
toStringin classObject- Returns:
- the content
- See Also:
format(int)
-
-