Class XmlDocument

java.lang.Object
com.oorian.xml.XmlDocument

public class XmlDocument extends Object
Represents an XML document with a root element and optional XML declaration.
  • Constructor Details

    • XmlDocument

      public XmlDocument()
      Constructs an empty XmlDocument with default version "1.0" and encoding "UTF-8".
    • XmlDocument

      public XmlDocument(XmlElement root)
      Constructs an XmlDocument with the specified root element.
      Parameters:
      root - the root element.
  • Method Details

    • parse

      public static XmlDocument parse(String xml) throws XmlParseException
      Parses an XML document from a string.
      Parameters:
      xml - The XML string to parse
      Returns:
      The parsed XmlDocument
      Throws:
      XmlParseException - If the XML is malformed
    • loadFromFile

      public static XmlDocument loadFromFile(String filePath) throws IOException, XmlParseException
      Loads an XML document from a file.
      Parameters:
      filePath - The path to the XML file
      Returns:
      The parsed XmlDocument
      Throws:
      IOException - If the file cannot be read
      XmlParseException - If the XML is malformed
    • loadFromFile

      public static XmlDocument loadFromFile(File file) throws IOException, XmlParseException
      Loads an XML document from a File object.
      Parameters:
      file - The file to load
      Returns:
      The parsed XmlDocument
      Throws:
      IOException - If the file cannot be read
      XmlParseException - If the XML is malformed
    • loadFromStream

      public static XmlDocument loadFromStream(InputStream inputStream) throws IOException, XmlParseException
      Loads an XML document from an InputStream.
      Parameters:
      inputStream - The input stream to read from
      Returns:
      The parsed XmlDocument
      Throws:
      IOException - If the stream cannot be read
      XmlParseException - If the XML is malformed
    • setVersion

      public void setVersion(String version)
      Sets the XML version declaration.
      Parameters:
      version - the version string (e.g., "1.0").
    • setEncoding

      public void setEncoding(String encoding)
      Sets the XML encoding declaration.
      Parameters:
      encoding - the encoding string (e.g., "UTF-8").
    • setRoot

      public void setRoot(XmlElement root)
      Sets the root element of this document.
      Parameters:
      root - the root element.
    • getVersion

      public String getVersion()
      Returns the XML version declaration.
      Returns:
      the version string.
    • getEncoding

      public String getEncoding()
      Returns the XML encoding declaration.
      Returns:
      the encoding string.
    • getRoot

      public XmlElement getRoot()
      Returns the root element of this document.
      Returns:
      the root element, or null if not set.
    • toXmlString

      public String toXmlString()
      Returns the XML as a formatted string with indentation.
      Returns:
      The formatted XML string
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • saveToFile

      public void saveToFile(String filePath) throws IOException
      Saves the document to a file.
      Parameters:
      filePath - The path to save to
      Throws:
      IOException - If the file cannot be written
    • saveToFile

      public void saveToFile(File file) throws IOException
      Saves the document to a File object.
      Parameters:
      file - The file to save to
      Throws:
      IOException - If the file cannot be written