Package com.oorian.xml
Class XmlDocument
java.lang.Object
com.oorian.xml.XmlDocument
Represents an XML document with a root element and optional XML declaration.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty XmlDocument with default version "1.0" and encoding "UTF-8".XmlDocument(XmlElement root) Constructs an XmlDocument with the specified root element. -
Method Summary
Modifier and TypeMethodDescriptionReturns the XML encoding declaration.getRoot()Returns the root element of this document.Returns the XML version declaration.static XmlDocumentloadFromFile(File file) Loads an XML document from a File object.static XmlDocumentloadFromFile(String filePath) Loads an XML document from a file.static XmlDocumentloadFromStream(InputStream inputStream) Loads an XML document from an InputStream.static XmlDocumentParses an XML document from a string.voidsaveToFile(File file) Saves the document to a File object.voidsaveToFile(String filePath) Saves the document to a file.voidsetEncoding(String encoding) Sets the XML encoding declaration.voidsetRoot(XmlElement root) Sets the root element of this document.voidsetVersion(String version) Sets the XML version declaration.toString()Returns the XML as a formatted string with indentation.
-
Constructor Details
-
XmlDocument
public XmlDocument()Constructs an empty XmlDocument with default version "1.0" and encoding "UTF-8". -
XmlDocument
Constructs an XmlDocument with the specified root element.- Parameters:
root- the root element.
-
-
Method Details
-
parse
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
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 readXmlParseException- If the XML is malformed
-
loadFromFile
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 readXmlParseException- 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 readXmlParseException- If the XML is malformed
-
setVersion
Sets the XML version declaration.- Parameters:
version- the version string (e.g., "1.0").
-
setEncoding
Sets the XML encoding declaration.- Parameters:
encoding- the encoding string (e.g., "UTF-8").
-
setRoot
Sets the root element of this document.- Parameters:
root- the root element.
-
getVersion
Returns the XML version declaration.- Returns:
- the version string.
-
getEncoding
Returns the XML encoding declaration.- Returns:
- the encoding string.
-
getRoot
Returns the root element of this document.- Returns:
- the root element, or null if not set.
-
toXmlString
Returns the XML as a formatted string with indentation.- Returns:
- The formatted XML string
-
toString
-
saveToFile
Saves the document to a file.- Parameters:
filePath- The path to save to- Throws:
IOException- If the file cannot be written
-
saveToFile
Saves the document to a File object.- Parameters:
file- The file to save to- Throws:
IOException- If the file cannot be written
-