Class XmlUtils


  • public class XmlUtils
    extends Object
    Utilities for handling XML-files.
    • Constructor Detail

      • XmlUtils

        public XmlUtils()
    • Method Detail

      • getXmlDoc

        public static org.dom4j.Document getXmlDoc​(File f)
                                            throws IOFailure
        Read and parse an XML-file, and return a Document object representing this object.
        Parameters:
        f - a given xml file
        Returns:
        a Document representing the xml file
        Throws:
        IOFailure - if unable to read the xml file or unable to parse the file as XML
      • getXmlDoc

        public static org.dom4j.Document getXmlDoc​(InputStream resourceAsStream)
        Read and parse an XML stream, and return a Document object representing this object.
        Parameters:
        resourceAsStream - a given xml document
        Returns:
        a Document representing the xml document
        Throws:
        IOFailure - if unable to read the xml document or unable to parse the document as XML
      • setNode

        public static void setNode​(org.dom4j.Document doc,
                                   String xpath,
                                   String value)
        Set a XmlNode defined by the given XPath to the given value.
        Parameters:
        doc - the Document, which is being modified
        xpath - the given XPath
        value - the given value
        Throws:
        IOFailure - If the given XPath was not found in the document
      • setNodes

        public static void setNodes​(org.dom4j.Document doc,
                                    String xpath,
                                    String value)
        Set a List of XmlNodes defined by the given XPath to the given value.
        Parameters:
        doc - the Document, which is being modified
        xpath - the given XPath
        value - the given value
        Throws:
        IOFailure - If the given XPath was not found in the document
      • validateWithXSD

        public static void validateWithXSD​(File xsdFile)
        Validate that the settings xml files conforms to the XSD.
        Parameters:
        xsdFile - Schema to check settings against.
        Throws:
        ArgumentNotValid - if unable to validate the settings files
        IOFailure - If unable to read the settings files and/or the xsd file.
      • writeXmlToFile

        public static void writeXmlToFile​(org.dom4j.Document doc,
                                          File f)
                                   throws IOFailure
        Write document tree to file.
        Parameters:
        doc - the document tree to save.
        f - the file to write the document to.
        Throws:
        IOFailure - On trouble writing XML file to disk.
      • documentFromString

        public static org.dom4j.Document documentFromString​(String xml)
                                                     throws org.dom4j.DocumentException
        Parses a given string to produce a Document instance.
        Parameters:
        xml - Some XML text.
        Returns:
        a Document parsed from the given xml.
        Throws:
        org.dom4j.DocumentException - If unable to parse the given text as XML.