Package processing.data
Class XML
java.lang.Object
processing.data.XML
- All Implemented Interfaces:
Serializable
XML is a representation of an XML object, able to parse XML code. Use
loadXML() to load external XML files and create XML
objects.
Only files encoded as UTF-8 (or plain ASCII) are parsed properly; the encoding parameter inside XML files is ignored.
Only files encoded as UTF-8 (or plain ASCII) are parsed properly; the encoding parameter inside XML files is ignored.
-
Constructor Summary
ConstructorDescriptionAdvanced users only; use loadXML() in PApplet.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.Advanced users only; use loadXML() in PApplet.Advanced users only; use loadXML() in PApplet. -
Method Summary
Modifier and TypeMethodDescriptionAppends a new child to the element.format
(int indent) Takes an XML object and converts it to a String, formatting its content as specified with the indent parameter.
If indent is set to -1, then the String is returned with no line breaks, no indentation, and no XML declaration.
If indent is set to 0 or greater, then the String is returned with line breaks, and the specified number of spaces as indent values.int
Counts the specified element's number of attributes, returned as an int.getChild
(int index) Returns the first of the element's children that matches the name parameter.Get a child by its name or path.int
Returns the number of children.XML[]
Returns all of the element's children as an array of XML objects.XML[]
getChildren
(String name) Get any children that match this name or path.Returns the content of an element.getContent
(String defaultValue) double
double
Returns the value of an attribute.double
double
getDoubleContent
(double defaultValue) float
Returns an attribute value of the element as a float.float
Returns the value of an attribute.float
Returns the content of an element as a float.float
getFloatContent
(float defaultValue) int
Returns an attribute value of the element as an int.int
Returns the value of an attribute.int
Returns the content of an element as an int.int
getIntContent
(int defaultValue) Returns the name of the element (without namespace prefix).long
Returns the value of an attribute.long
long
getLongContent
(long defaultValue) getName()
Gets the element's full name, which is returned as a String.Gets a copy of the element's parent.Returns an attribute value of the element as a String.boolean
hasAttribute
(String name) Checks whether or not an element has the specified attribute.boolean
Checks whether or not the element has any children, and returns the result as a boolean.String[]
Gets all of the specified element's attributes, and returns them as an array of Strings.String[]
Get the names of all of the element's children, and returns the names as an array of Strings.static XML
Converts String content to an XML objectstatic XML
void
print()
void
removeChild
(XML kid) Removes the specified element.boolean
boolean
void
setContent
(String text) Sets the element's content, which is specified as a String.void
void
setDoubleContent
(double value) void
Sets the content of an element's attribute as a float.void
setFloatContent
(float value) void
Sets the content of an element's attribute as an int.void
setIntContent
(int value) void
Sets the content of an element as an intvoid
setLongContent
(long value) void
Sets the element's name, which is specified as a String.void
Sets the content of an element's attribute as a String.toString()
Takes an XML object and converts it to a String, using default formatting rules (includes an XML declaration, line breaks, and two spaces for indents).void
trim()
Removes whitespace nodes.boolean
write
(PrintWriter output)
-
Constructor Details
-
XML
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
-
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
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
- Parameters:
name
- creates a node with this name
-
-
Method Details
-
parse
Converts String content to an XML object- Parameters:
data
- the content to be parsed as XML- Returns:
- an XML object, or null
- Throws:
SAXException
ParserConfigurationException
IOException
-
parse
public static XML parse(String data, String options) throws IOException, ParserConfigurationException, SAXException -
save
-
save
-
write
-
getParent
Gets a copy of the element's parent. Returns the parent as another XML object. -
getName
Gets the element's full name, which is returned as a String.- Returns:
- the name, or null if the element only contains #PCDATA.
-
setName
Sets the element's name, which is specified as a 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()Checks whether or not the element has any children, and returns the result as a boolean. -
listChildren
Get the names of all of the element's children, and returns the names as an array of Strings. This is the same as looping through and calling getName() on each child element individually. -
getChildren
Returns all of the element's children as an array of XML objects. When the name parameter is specified, then it will return all children that match that name or path. The path is a series of elements and sub-elements, separated by slashes. -
getChild
Returns the first of the element's children that matches the name parameter. The name or path is a series of elements and sub-elements, separated by slashes. -
getChild
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
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
Appends a new child to the element. The child can be specified with either a String, which will be used as the new tag's name, or as a reference to an existing XML object.
A reference to the newly created child is returned as an XML object. -
addChild
-
removeChild
Removes the specified element. First use getChild() to get a reference to the desired element. Then pass that reference to removeChild() to delete it. -
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()Counts the specified element's number of attributes, returned as an int. -
listAttributes
Gets all of the specified element's attributes, and returns them as an array of Strings. -
hasAttribute
Checks whether or not an element has the specified attribute. The attribute must be specified as a String, and a boolean is returned. -
getString
Returns an attribute value of the element as a String. If the defaultValue parameter is specified and the attribute doesn't exist, then defaultValue is returned. If no defaultValue is specified and the attribute doesn't exist, null is returned. -
getString
-
setString
Sets the content of an element's attribute as a String. The first String specifies the attribute name, while the second specifies the new content. -
getInt
Returns an attribute value of the element as an int. If the defaultValue parameter is specified and the attribute doesn't exist, then defaultValue is returned. If no defaultValue is specified and the attribute doesn't exist, the value 0 is returned. -
setInt
Sets the content of an element's attribute as an int. A String specifies the attribute name, while the int specifies the new content. -
getInt
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
Sets the content of an element as an int -
getLong
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
Returns an attribute value of the element as a float. If the defaultValue parameter is specified and the attribute doesn't exist, then defaultValue is returned. If no defaultValue is specified and the attribute doesn't exist, the value 0.0 is returned. -
getFloat
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
Sets the content of an element's attribute as a float. A String specifies the attribute name, while the float specifies the new content. -
getDouble
-
getDouble
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
-
getContent
Returns the content of an element. If there is no such content, null is returned.- Returns:
- the content.
- See Also:
-
getContent
-
getIntContent
public int getIntContent()Returns the content of an element as an int. If there is no such content, either null or the provided default value is returned.- Returns:
- the content.
- See Also:
-
getIntContent
public int getIntContent(int defaultValue) - Parameters:
defaultValue
- the default value of the attribute
-
getFloatContent
public float getFloatContent()Returns the content of an element as a float. If there is no such content, either null or the provided default value is returned.- Returns:
- the content.
- See Also:
-
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
Sets the element's content, which is specified as a String. -
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
Takes an XML object and converts it to a String, formatting its content as specified with the indent parameter.
If indent is set to -1, then the String is returned with no line breaks, no indentation, and no XML declaration.
If indent is set to 0 or greater, then the String is returned with line breaks, and the specified number of spaces as indent values. Meaning, there will be no indentation if 0 is specified, or each indent will be replaced with the corresponding number of spaces: 1, 2, 3, and so on.- Parameters:
indent
- -1 for a single line (and no declaration), >= 0 for indents and newlines- Returns:
- the content
- See Also:
-
print
public void print() -
toString
Takes an XML object and converts it to a String, using default formatting rules (includes an XML declaration, line breaks, and two spaces for indents). These are the same formatting rules used by println() when printing an XML object. This method produces the same results as using format(2).
-