processing.data
Class XML
- java.lang.Object
-
- processing.data.XML
-
- All Implemented Interfaces:
- java.io.Serializable
public class XML extends java.lang.Object implements java.io.SerializableThis is the base class used for the Processing XML library, representing a single node of an XML tree.
-
-
Constructor Summary
Constructors Constructor and Description XML(java.io.File file)Advanced users only; use loadXML() in PApplet.XML(java.io.File file, java.lang.String options)Advanced users only; use loadXML() in PApplet.XML(java.io.InputStream input)XML(java.io.InputStream input, java.lang.String options)Unlike the loadXML() method in PApplet, this version works with files that are not in UTF-8 format.XML(java.io.Reader reader)Advanced users only; use loadXML() in PApplet.XML(java.io.Reader reader, java.lang.String options)Advanced users only; use loadXML() in PApplet.XML(java.lang.String name)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description XMLaddChild(java.lang.String tag)XMLaddChild(XML child)java.lang.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(java.lang.String name)Get a child by its name or path.intgetChildCount()Returns the number of children.XML[]getChildren()Returns an array containing all the child elements.XML[]getChildren(java.lang.String name)Get any children that match this name or path.java.lang.StringgetContent()Return the #PCDATA content of the element.java.lang.StringgetContent(java.lang.String defaultValue)doublegetDouble(java.lang.String name)doublegetDouble(java.lang.String name, double defaultValue)Returns the value of an attribute.doublegetDoubleContent()doublegetDoubleContent(double defaultValue)floatgetFloat(java.lang.String name)Returns the value of an attribute, or zero if not present.floatgetFloat(java.lang.String name, float defaultValue)Returns the value of an attribute.floatgetFloatContent()floatgetFloatContent(float defaultValue)intgetInt(java.lang.String name)intgetInt(java.lang.String name, int defaultValue)Returns the value of an attribute.intgetIntContent()intgetIntContent(int defaultValue)java.lang.StringgetLocalName()Returns the name of the element (without namespace prefix).longgetLong(java.lang.String name, long defaultValue)Returns the value of an attribute.longgetLongContent()longgetLongContent(long defaultValue)java.lang.StringgetName()Returns the full name (i.e.XMLgetParent()Returns the parent element.java.lang.StringgetString(java.lang.String name)java.lang.StringgetString(java.lang.String name, java.lang.String defaultValue)booleanhasAttribute(java.lang.String name)Returns whether an attribute exists.booleanhasChildren()Returns a boolean of whether or not there are children.java.lang.String[]listAttributes()Get a list of the names for all of the attributes for this node.java.lang.String[]listChildren()Put the names of all children into an array.static XMLparse(java.lang.String data)static XMLparse(java.lang.String data, java.lang.String options)voidremoveChild(XML kid)booleansave(java.io.File file)booleansave(java.io.File file, java.lang.String options)voidsetContent(java.lang.String text)voidsetDouble(java.lang.String name, double value)voidsetDoubleContent(double value)voidsetFloat(java.lang.String name, float value)voidsetFloatContent(float value)voidsetInt(java.lang.String name, int value)voidsetIntContent(int value)voidsetLong(java.lang.String name, long value)voidsetLongContent(long value)voidsetName(java.lang.String newName)voidsetString(java.lang.String name, java.lang.String value)java.lang.StringtoString()Return the XML document formatted with two spaces for indents.voidtrim()booleanwrite(java.io.PrintWriter output)
-
-
-
Constructor Detail
-
XML
public XML(java.io.File file) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXExceptionAdvanced 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.- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
XML
public XML(java.io.File file, java.lang.String options) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXExceptionAdvanced users only; use loadXML() in PApplet.- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
XML
public XML(java.io.InputStream input) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
XML
public XML(java.io.InputStream input, java.lang.String options) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXExceptionUnlike the loadXML() method in PApplet, this version works with files that are not in UTF-8 format.- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
XML
public XML(java.io.Reader reader) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXExceptionAdvanced users only; use loadXML() in PApplet.- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
XML
public XML(java.io.Reader reader, java.lang.String options) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXExceptionAdvanced 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- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
XML
public XML(java.lang.String name)
- Parameters:
name- creates a node with this name
-
-
Method Detail
-
parse
public static XML parse(java.lang.String data) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
- Parameters:
data- the content to be parsed as XML- Returns:
- an XML object, or null
- Throws:
org.xml.sax.SAXExceptionjavax.xml.parsers.ParserConfigurationExceptionjava.io.IOException
-
parse
public static XML parse(java.lang.String data, java.lang.String options) throws java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
- Throws:
java.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
save
public boolean save(java.io.File file)
-
save
public boolean save(java.io.File file, java.lang.String options)
-
write
public boolean write(java.io.PrintWriter output)
-
getParent
public XML 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.
-
setName
public void setName(java.lang.String newName)
-
getLocalName
public java.lang.String getLocalName()
Returns the name of the element (without namespace prefix). 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 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 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(java.lang.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
-
getChildren
public XML[] 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
-
addChild
public XML addChild(java.lang.String tag)
-
removeChild
public void removeChild(XML kid)
-
trim
public void trim()
-
getAttributeCount
public int getAttributeCount()
Returns the number of attributes.
-
listAttributes
public java.lang.String[] listAttributes()
Get a list of the names for all of the attributes for this node.
-
hasAttribute
public boolean hasAttribute(java.lang.String name)
Returns whether an attribute exists.
-
getString
public java.lang.String getString(java.lang.String name)
-
getString
public java.lang.String getString(java.lang.String name, java.lang.String defaultValue)
-
setString
public void setString(java.lang.String name, java.lang.String value)
-
getInt
public int getInt(java.lang.String name)
-
setInt
public void setInt(java.lang.String name, int value)
-
getInt
public int getInt(java.lang.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(java.lang.String name, long value)
-
getLong
public long getLong(java.lang.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(java.lang.String name)
Returns the value of an attribute, or zero if not present.
-
getFloat
public float getFloat(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.
-
setFloat
public void setFloat(java.lang.String name, float value)
-
getDouble
public double getDouble(java.lang.String name)
-
getDouble
public double getDouble(java.lang.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(java.lang.String name, double value)
-
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.
- See Also:
getIntContent(),getFloatContent()
-
getContent
public java.lang.String getContent(java.lang.String defaultValue)
-
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(java.lang.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 java.lang.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()
-
toString
public java.lang.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 classjava.lang.Object- Returns:
- the content
- See Also:
format(int)
-
-
DataMelt 3.0 © DataMelt by jWork.ORG