Package javaforce

Class XML

java.lang.Object
javaforce.XML

public class XML extends Object
XML encapsules a complete XML file.
Each XML tag (element) is treated as a node in the tree. The read() functions include a callback interface so you can further tweak the layout of the XML tree.
Then you can write() it back to a file. Typical XML Tag:
<name [attributes...]> content | children </name>
Singleton XML Tag: (no children)
<name [attributes...] />
Caveats:
Only leaf nodes can contain actual data (content) (in other words @XmlMixed is not supported).
Mixed tags are read, but when written the content is lost.
There must be only one root tag.
Support for the standard XML header is provided (see header).
readClass() and writeClass() support : int, short, byte, float, double, boolean, String, and Custom Classes.
Arrays of any of these.
All classes and fields MUST be public. static and transient members are skipped. No special annotations are required.
  • Field Details

  • Constructor Details

    • XML

      public XML()
      Constructs a new XML object.
  • Method Details

    • read

      public boolean read(String filename)
      Reads the entire tree from a XML file from filename.
      Parameters:
      filename - name of file to load XML data from
    • read

      public boolean read(InputStream is)
      Reads the entire tree from a XML file from the InputStream.
      Parameters:
      is - InputStream to load XML data from
    • write

      public boolean write(String filename)
      Writes the entire tree as a XML file to the filename.
    • write

      public boolean write(OutputStream os)
      Writes the entire tree as a XML file to the OutputStream.
    • deleteAll

      public void deleteAll()
      Deletes the entire tree and resets the root and header tags.
    • deleteTag

      public boolean deleteTag(XML.XMLTag tag)
      Deletes a node from the parent. Also deletes all it's children.
    • createTag

      public XML.XMLTag createTag()
      Creates an empty node that can be inserted into the tree using addTag().
    • addTag

      public XML.XMLTag addTag(XML.XMLTag parent, XML.XMLTag tag)
      Adds the node to the targetParent.
    • addTag

      public XML.XMLTag addTag(XML.XMLTag targetParent, String name, String attrs, String content)
      Adds node with the name, attrs and content specified. If another node already exists with the same name the new node's unique name will differ.
    • addSetTag

      public XML.XMLTag addSetTag(XML.XMLTag targetParent, String name, String attrs, String content)
      Adds (a non-existing) or sets (an existing) node with the name, attrs and content specified.
    • setRoot

      public void setRoot(String name, String attrs, String content)
      Sets the name, attrs and contents of the true root node.
    • getName

      public String getName(XML.XMLTag tag)
      Returns the unique name of a node.
    • setName

      public void setName(XML.XMLTag tag, String newName)
      Sets the name of a node. It's unique name may differ when shown in a tree.
    • getTag

      public XML.XMLTag getTag(Object[] objs)
      Returns a node based on the objs[] path.
    • setTag

      public void setTag(XML.XMLTag tag, String name, String attrs, String content)
      Sets the name, attrs and content for an existing node.
    • writeClass

      public void writeClass(XML.XMLTag tag, Object obj)
      Writes all children of tag to object.
    • writeClass

      public void writeClass(Object obj)
      Writes entire XML tree to object.
    • readClass

      public void readClass(XML.XMLTag tag, Object obj)
      Reads all fields from an object and places into tag.
    • readClass

      public void readClass(String rootName, Object obj)
      Reads entire XML tree from object (deletes entire tree first).
      Parameters:
      rootName - = name to assign to root tag.