Class XML

java.lang.Object
processing.data.XML
All Implemented Interfaces:
Serializable

public class XML extends Object implements 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.
See Also:
  • Constructor Details

  • Method Details

    • parse

      public static XML parse(String data) throws IOException, ParserConfigurationException, SAXException
      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
      Throws:
      IOException
      ParserConfigurationException
      SAXException
    • save

      public boolean save(File file)
    • save

      public boolean save(File file, String options)
    • write

      public boolean write(PrintWriter output)
    • getParent

      public XML getParent()
      Gets a copy of the element's parent. Returns the parent as another XML object.
    • getName

      public String 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

      public void setName(String newName)
      Sets the element's name, which is specified as a String.
    • getLocalName

      public 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

      public String[] 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

      public XML[] 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

      public XML getChild(int index)
      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

      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
    • 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
    • addChild

      public XML addChild(String tag)
      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

      public XML addChild(XML child)
    • removeChild

      public void removeChild(XML kid)
      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

      public String[] listAttributes()
      Gets all of the specified element's attributes, and returns them as an array of Strings.
    • hasAttribute

      public boolean hasAttribute(String name)
      Checks whether or not an element has the specified attribute. The attribute must be specified as a String, and a boolean is returned.
    • getString

      public String getString(String name)
      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

      public String getString(String name, String defaultValue)
    • setString

      public void setString(String name, String value)
      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

      public int getInt(String name)
      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

      public void setInt(String name, int value)
      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

      public int getInt(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
    • setLong

      public void setLong(String name, long value)
      Sets the content of an element as an int
    • 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 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

      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)
      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

      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 attribute
      defaultValue - 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()
      Returns the content of an element. If there is no such content, null is returned.
      Returns:
      the content.
      See Also:
    • getContent

      public String getContent(String defaultValue)
    • 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

      public void setContent(String text)
      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

      public String 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. 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

      public 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). These are the same formatting rules used by println() when printing an XML object. This method produces the same results as using format(2).
      Overrides:
      toString in class Object
      Returns:
      the content
      See Also: