Class XMLUtils


  • public final class XMLUtils
    extends java.lang.Object
    Contains various utility function to parse and output XML.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.dom4j.QName SCHEMA_LOCATION
      QName for XSI.
      static org.dom4j.Namespace XSI
      XMLSchema-instance uri.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected XMLUtils()
      The default constructor (hidden).
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void changeNamespace​(org.dom4j.Element el, org.dom4j.Namespace namespace)
      Traverse the element to the roots and updates the namespace
      static org.dom4j.Document createDocument()
      Creates an empty DOM4J Document.
      static org.dom4j.Document createDocument​(java.io.File file)
      Creates a DOM4J Document from an XML file
      static org.dom4j.Document createDocument​(java.io.File file, org.xml.sax.EntityResolver resolver)
      Creates a DOM4J Document from a file, with optional entity resolver
      static org.dom4j.Document createDocument​(java.io.File file, org.xml.sax.EntityResolver resolver, boolean validate)
      Creates a DOM4J Document from a file, with optional entity resolver and option to validate the XML
      static org.dom4j.Document createDocument​(java.io.InputStream stream)
      Creates a DOM4J Document from a given input stream.
      static org.dom4j.Document createDocument​(java.io.InputStream stream, org.xml.sax.EntityResolver resolver)
      Creates a DOM4J Document from a given input stream, with an optional entity resolver.
      static org.dom4j.Document createDocument​(java.lang.String xml)
      Creates a DOM4J Document from the passed in string.
      static org.dom4j.Document createDocument​(java.lang.String xmlString, org.xml.sax.EntityResolver resolver)
      Parses a string into a DOM4J Document.
      static org.dom4j.Document createDocument​(org.w3c.dom.Document dom)
      Creates a DOM4J Document from the passed in W3C Document.
      static org.w3c.dom.Document createDocumentW3C​(org.dom4j.Document doc)
      Creates a W3C Document from a given DOM4J Document.
      static org.dom4j.Element createElement​(java.lang.String name)
      Creates a DOM4J Element with a given name.
      static org.w3c.dom.Element createElement​(org.dom4j.Element elem)
      Creates a W3C Element from a given DOM4J Element.
      static org.dom4j.Element createElement​(org.w3c.dom.Element elem)
      Creates a DOM4J Element from a given W3C Element.
      static org.w3c.dom.Document createW3cDocument()
      Creates a new W3C Document
      static java.lang.String escape​(java.lang.String s)
      Escapes a string for XML.
      static boolean getBoolean​(org.dom4j.Element elem, java.lang.String attrName, boolean defValue)
      Gets the boolean value of an attribute on the given element
      static java.util.List<org.dom4j.Element> getElements​(org.dom4j.Node elem, java.lang.String xpath)
      Runs a given XPath on XML pointed to by the given node.
      static org.dom4j.Element getFirstElement​(org.dom4j.Element elem, java.lang.String xpath)
      Gets the first element matching the given XPath
      static org.dom4j.Node getFirstNode​(org.dom4j.Node elem, java.lang.String xpath)
      Gets the first node matching the given XPath
      static java.lang.String getFirstNodeValue​(org.dom4j.Node elem, java.lang.String xpath)
      Gets the value of the first node matching a given XPath
      static java.lang.String getFirstNodeValue​(org.dom4j.Node elem, java.lang.String xpath, java.lang.String def)
      Gets the value of the first node matching a given XPath, with a default value provided
      static int getInteger​(org.dom4j.Element elem, java.lang.String attrName, int defaultValue)
      Gets the int value of an attribute on the given element
      static java.util.List<org.dom4j.Node> getNodes​(org.dom4j.Node elem, java.lang.String xpath)
      Runs a given XPath on XML pointed to by the given node.
      static int getSAXReaderPoolCount()  
      static java.lang.String joinPath​(java.lang.String... args)
      Joins multiple XPaths together (joining on "/") NOTE: if an XPath begins with "/" it will re-root the XPath (previous XPaths will be removed)
      static void mergeSchemaLocations​(org.dom4j.Document src, org.dom4j.Document dest)
      Merge all the xsd schema loations from one doc to another.
      static org.dom4j.Document parse​(java.io.InputStream stream, boolean validate)
      Parses XML from the passed in input stream, with option to validate.
      static org.dom4j.Document parse​(java.io.Reader r, boolean validate)
      Parses XML from the passed in reader, with option to validate.
      static org.dom4j.Document parse​(java.lang.String xml)
      Parses XML from the passed in string.
      static org.dom4j.Document parse​(java.lang.String xml, boolean validate)
      Parses XML from the passed in string, with option to validate.
      static org.dom4j.Document parse​(org.xml.sax.InputSource source, boolean validate)
      Parses XML from the passed in InputSource, with option to validate.
      static org.w3c.dom.Document parseAsW3c​(java.io.File xml)  
      static org.w3c.dom.Document parseAsW3c​(java.io.File xml, boolean validate)  
      static org.w3c.dom.Document parseAsW3c​(java.io.InputStream xml)  
      static org.w3c.dom.Document parseAsW3c​(java.io.InputStream xml, boolean validate)  
      static org.w3c.dom.Document parseAsW3c​(java.lang.String xml)  
      static org.w3c.dom.Document parseAsW3c​(java.lang.String xml, boolean validate)  
      static org.w3c.dom.Document parseAsW3c​(org.xml.sax.InputSource xml, boolean validate)  
      static void prettyPrint​(java.io.Writer w, org.dom4j.Node n)
      Outputs the string representation of the XML document to the given writer.
      static void prettyPrint​(java.io.Writer w, org.dom4j.Node n, boolean close)
      Ouptuts the string representation of the XML document to the given writer.
      static java.lang.String prettyPrint​(org.dom4j.Node n)
      Gets the string representation of the given Node.
      static java.lang.String prettyPrint​(org.w3c.dom.Node n)  
      static java.lang.String prettyPrint​(org.w3c.dom.Node n, int indent)  
      static java.lang.String resolvePath​(java.lang.String... args)
      Resolves an XPath(s), removing .
      static java.util.List<org.dom4j.Element> selectElements​(org.dom4j.Node elem, java.lang.String xpath)
      Runs an XPath expression
      static java.util.List<?> selectNodes​(org.w3c.dom.Node n, java.lang.String xpath)  
      static java.util.List<org.dom4j.Element> selectNodesInNodeNamespace​(org.dom4j.Node elem, java.lang.String xpath)
      Sets up a namespace context for the xpath, mapping the prefix "def" to the namespace of the document for the node.
      static java.lang.Object selectSingleNode​(org.w3c.dom.Node n, java.lang.String xpath)  
      static org.dom4j.Element selectSingleNodeInNodeNamespace​(org.dom4j.Node elem, java.lang.String xpath)  
      static java.lang.String[] splitAttribute​(java.lang.String path)
      Splits "/hello/world/@attr" into { "/hello/world", "attr" }.
      static java.lang.String[] splitLast​(java.lang.String path, char sep)
      Splits off the last element.
      static void transform​(java.lang.Object input, java.io.InputStream xsltFile, java.io.OutputStream out)
      Transform a Object using the XMLSerializer into an OutputStream via an XSLT stylesheet.
      static void writeXmlToFile​(java.io.File file, org.dom4j.Document document, org.dom4j.io.OutputFormat format)
      Writes XML to file.
      static void writeXmlToStream​(java.io.OutputStream stream, org.dom4j.Document document, org.dom4j.io.OutputFormat format)
      Writes XML to a stream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • XSI

        public static final org.dom4j.Namespace XSI
        XMLSchema-instance uri.
      • SCHEMA_LOCATION

        public static final org.dom4j.QName SCHEMA_LOCATION
        QName for XSI.
    • Constructor Detail

      • XMLUtils

        protected XMLUtils()
        The default constructor (hidden).
    • Method Detail

      • createDocument

        public static org.dom4j.Document createDocument()
        Creates an empty DOM4J Document.
        Returns:
        a DOM4J Document
      • createDocument

        public static org.dom4j.Document createDocument​(org.w3c.dom.Document dom)
        Creates a DOM4J Document from the passed in W3C Document.
        Parameters:
        dom - the DOM4J Document
        Returns:
        the W3C Document
      • createDocument

        public static org.dom4j.Document createDocument​(java.lang.String xml)
                                                 throws org.dom4j.DocumentException
        Creates a DOM4J Document from the passed in string.
        Parameters:
        xml - the XML string
        Returns:
        the DOM4J Document
        Throws:
        org.dom4j.DocumentException
      • createDocument

        public static org.dom4j.Document createDocument​(java.lang.String xmlString,
                                                        org.xml.sax.EntityResolver resolver)
                                                 throws org.dom4j.DocumentException
        Parses a string into a DOM4J Document.
        Parameters:
        xmlString - the XML string
        resolver - the entity resolver
        Returns:
        the DOM4J Document
        Throws:
        org.dom4j.DocumentException
      • createDocument

        public static org.dom4j.Document createDocument​(java.io.File file)
                                                 throws java.io.FileNotFoundException,
                                                        org.dom4j.DocumentException
        Creates a DOM4J Document from an XML file
        Parameters:
        file - the file
        Returns:
        the Document
        Throws:
        java.io.FileNotFoundException
        org.dom4j.DocumentException
      • createDocument

        public static org.dom4j.Document createDocument​(java.io.File file,
                                                        org.xml.sax.EntityResolver resolver)
                                                 throws java.io.FileNotFoundException,
                                                        org.dom4j.DocumentException
        Creates a DOM4J Document from a file, with optional entity resolver
        Parameters:
        file - the file
        resolver - the entity resolver
        Returns:
        the Document
        Throws:
        java.io.FileNotFoundException
        org.dom4j.DocumentException
      • createDocument

        public static org.dom4j.Document createDocument​(java.io.File file,
                                                        org.xml.sax.EntityResolver resolver,
                                                        boolean validate)
                                                 throws java.io.FileNotFoundException,
                                                        org.dom4j.DocumentException
        Creates a DOM4J Document from a file, with optional entity resolver and option to validate the XML
        Parameters:
        file - the file
        resolver - the entity resolver
        validate - if true, validation is to be performed
        Returns:
        the Document
        Throws:
        java.io.FileNotFoundException
        org.dom4j.DocumentException
      • createDocument

        public static org.dom4j.Document createDocument​(java.io.InputStream stream)
                                                 throws org.dom4j.DocumentException
        Creates a DOM4J Document from a given input stream.
        Parameters:
        stream - the input stream
        Returns:
        the Document
        Throws:
        org.dom4j.DocumentException
      • createDocument

        public static org.dom4j.Document createDocument​(java.io.InputStream stream,
                                                        org.xml.sax.EntityResolver resolver)
                                                 throws org.dom4j.DocumentException
        Creates a DOM4J Document from a given input stream, with an optional entity resolver.
        Parameters:
        stream - the stream
        resolver - the resolver
        Returns:
        the Document
        Throws:
        org.dom4j.DocumentException
      • createDocumentW3C

        public static org.w3c.dom.Document createDocumentW3C​(org.dom4j.Document doc)
        Creates a W3C Document from a given DOM4J Document.
        Parameters:
        doc - the DOM4J Document
        Returns:
        the W3C Document
        Throws:
        org.dom4j.DocumentException
      • createElement

        public static org.dom4j.Element createElement​(org.w3c.dom.Element elem)
        Creates a DOM4J Element from a given W3C Element.
        Parameters:
        elem - W3C Element
        Returns:
        the DOM4J Element
      • createElement

        public static org.w3c.dom.Element createElement​(org.dom4j.Element elem)
        Creates a W3C Element from a given DOM4J Element.
        Parameters:
        elem - the DOM4J Element
        Returns:
        the W3C Element
      • parse

        public static org.dom4j.Document parse​(java.lang.String xml)
                                        throws org.dom4j.DocumentException
        Parses XML from the passed in string.
        Parameters:
        xml - string to parse
        Returns:
        the parsed document
        Throws:
        org.dom4j.DocumentException - if xml parsing fails
      • parse

        public static org.dom4j.Document parse​(java.io.InputStream stream,
                                               boolean validate)
                                        throws org.dom4j.DocumentException
        Parses XML from the passed in input stream, with option to validate.
        Parameters:
        stream - stream to parse
        validate - if true, validation is to be performed
        Returns:
        the parsed document
        Throws:
        org.dom4j.DocumentException - if xml parsing fails
      • parse

        public static org.dom4j.Document parse​(org.xml.sax.InputSource source,
                                               boolean validate)
                                        throws org.dom4j.DocumentException
        Parses XML from the passed in InputSource, with option to validate.
        Parameters:
        source - input source to parse
        validate - if true, validation is to be performed
        Returns:
        the parsed document
        Throws:
        org.dom4j.DocumentException - if xml parsing fails
      • parse

        public static org.dom4j.Document parse​(java.io.Reader r,
                                               boolean validate)
                                        throws org.dom4j.DocumentException
        Parses XML from the passed in reader, with option to validate.
        Parameters:
        r - the Reader to parse from
        validate - if true, validation is to be performed
        Returns:
        the parsed document
        Throws:
        org.dom4j.DocumentException - if xml parsing fails
      • parse

        public static org.dom4j.Document parse​(java.lang.String xml,
                                               boolean validate)
                                        throws org.dom4j.DocumentException
        Parses XML from the passed in string, with option to validate.
        Parameters:
        xml - string to parse
        validate - if true, validation is to be performed
        Returns:
        the parsed document
        Throws:
        org.dom4j.DocumentException - if xml parsing fails
      • prettyPrint

        public static void prettyPrint​(java.io.Writer w,
                                       org.dom4j.Node n)
                                throws java.io.IOException
        Outputs the string representation of the XML document to the given writer.
        Parameters:
        w - write to write XML to
        n - Node to write
        Throws:
        java.io.IOException - if writing XML fails
      • prettyPrint

        public static void prettyPrint​(java.io.Writer w,
                                       org.dom4j.Node n,
                                       boolean close)
                                throws java.io.IOException
        Ouptuts the string representation of the XML document to the given writer.
        Parameters:
        w - write to write XML to
        n - Node to write
        close - close the writer
        Throws:
        java.io.IOException - if writing XML fails
      • prettyPrint

        public static java.lang.String prettyPrint​(org.dom4j.Node n)
        Gets the string representation of the given Node.
        Parameters:
        n - Node to print
        Returns:
        string representation of the Node
      • escape

        public static java.lang.String escape​(java.lang.String s)
        Escapes a string for XML.
        Parameters:
        s - string to escape. Must not be null.
        Returns:
        s, escaped for XML
      • resolvePath

        public static java.lang.String resolvePath​(java.lang.String... args)
        Resolves an XPath(s), removing . and .. entries if possible.
        Parameters:
        args - xpath(s) to resolve (resolving . and .. entries).
        Returns:
        path removing . and .. entries (as interned String)
      • splitAttribute

        public static java.lang.String[] splitAttribute​(java.lang.String path)
        Splits "/hello/world/@attr" into { "/hello/world", "attr" }.
      • splitLast

        public static java.lang.String[] splitLast​(java.lang.String path,
                                                   char sep)
        Splits off the last element.
        Returns:
        string array of length 2 (if sep not found, second element is null).
      • joinPath

        public static java.lang.String joinPath​(java.lang.String... args)
        Joins multiple XPaths together (joining on "/") NOTE: if an XPath begins with "/" it will re-root the XPath (previous XPaths will be removed)
        Parameters:
        args - the XPath(s)
        Returns:
        the result
      • createW3cDocument

        public static org.w3c.dom.Document createW3cDocument()
        Creates a new W3C Document
        Returns:
        the newly created W3C Document
        Throws:
        javax.xml.parsers.ParserConfigurationException
      • createElement

        public static org.dom4j.Element createElement​(java.lang.String name)
        Creates a DOM4J Element with a given name.
        Parameters:
        name - the element name
        Returns:
        the newly created element
      • writeXmlToStream

        public static void writeXmlToStream​(java.io.OutputStream stream,
                                            org.dom4j.Document document,
                                            org.dom4j.io.OutputFormat format)
                                     throws java.io.IOException
        Writes XML to a stream
        Parameters:
        stream - the stream to write to
        document - the document to write
        format - the output format to use
        Throws:
        java.io.IOException
      • writeXmlToFile

        public static void writeXmlToFile​(java.io.File file,
                                          org.dom4j.Document document,
                                          org.dom4j.io.OutputFormat format)
                                   throws java.io.IOException
        Writes XML to file.
        Parameters:
        file - the file to write to
        document - the document to write
        format - the output format to use
        Throws:
        java.io.IOException
      • selectElements

        public static java.util.List<org.dom4j.Element> selectElements​(org.dom4j.Node elem,
                                                                       java.lang.String xpath)
        Runs an XPath expression
        Parameters:
        elem - the starting element
        xpath - the XPath
        Returns:
        the results
      • getFirstElement

        public static org.dom4j.Element getFirstElement​(org.dom4j.Element elem,
                                                        java.lang.String xpath)
        Gets the first element matching the given XPath
        Parameters:
        elem - the element to start searching from
        xpath - the XPath
        Returns:
        the result (null if not found)
      • getNodes

        public static java.util.List<org.dom4j.Node> getNodes​(org.dom4j.Node elem,
                                                              java.lang.String xpath)
        Runs a given XPath on XML pointed to by the given node.
        Parameters:
        elem - the node to start from
        xpath - the XPath
        Returns:
        the results
      • getElements

        public static java.util.List<org.dom4j.Element> getElements​(org.dom4j.Node elem,
                                                                    java.lang.String xpath)
        Runs a given XPath on XML pointed to by the given node.
        Parameters:
        elem - the node to start from
        xpath - the XPath
        Returns:
        the results
      • selectNodesInNodeNamespace

        public static java.util.List<org.dom4j.Element> selectNodesInNodeNamespace​(org.dom4j.Node elem,
                                                                                   java.lang.String xpath)
        Sets up a namespace context for the xpath, mapping the prefix "def" to the namespace of the document for the node. This makes using xpath for the paths in the default namespace simpler. See http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html and the section on Namespace contexts for details. Xpaths provided to this method must use the "def" prefix to match nodes in the default namespace of the document. For instance, if the source document is:
         <services xmlns="http://www.attivio.com/configuration/type/servicesType"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.attivio.com/configuration/type/servicesType http://www.attivio.com/configuration/type/servicesType.xsd ">
         <service name="scheduler"/>
         </services>
         
        To find the service node, one would use xpath:
        /def:services/def:service
        Parameters:
        elem -
        xpath -
        Returns:
        matching elements
      • selectSingleNodeInNodeNamespace

        public static org.dom4j.Element selectSingleNodeInNodeNamespace​(org.dom4j.Node elem,
                                                                        java.lang.String xpath)
        Parameters:
        elem -
        xpath -
        Returns:
      • getFirstNode

        public static org.dom4j.Node getFirstNode​(org.dom4j.Node elem,
                                                  java.lang.String xpath)
        Gets the first node matching the given XPath
        Parameters:
        elem - the node to start searching from
        xpath - the XPath
        Returns:
        the first match or null if none found
      • getFirstNodeValue

        public static java.lang.String getFirstNodeValue​(org.dom4j.Node elem,
                                                         java.lang.String xpath)
        Gets the value of the first node matching a given XPath
        Parameters:
        elem - the node
        xpath - the XPath
        Returns:
        the value
      • getFirstNodeValue

        public static java.lang.String getFirstNodeValue​(org.dom4j.Node elem,
                                                         java.lang.String xpath,
                                                         java.lang.String def)
        Gets the value of the first node matching a given XPath, with a default value provided
        Parameters:
        elem - the node
        xpath - the XPath
        def - the optional default value
        Returns:
        the value or def if not found
      • getBoolean

        public static boolean getBoolean​(org.dom4j.Element elem,
                                         java.lang.String attrName,
                                         boolean defValue)
        Gets the boolean value of an attribute on the given element
        Parameters:
        elem - the element
        attrName - the attribute name
        defValue - the default value
        Returns:
        the attribute value or defValue if not found
      • getInteger

        public static int getInteger​(org.dom4j.Element elem,
                                     java.lang.String attrName,
                                     int defaultValue)
        Gets the int value of an attribute on the given element
        Parameters:
        elem - the element
        attrName - the attribute name
        defaultValue - the default value
        Returns:
        the attribute value or defaultValue if not found
      • transform

        public static void transform​(java.lang.Object input,
                                     java.io.InputStream xsltFile,
                                     java.io.OutputStream out)
                              throws java.io.IOException
        Transform a Object using the XMLSerializer into an OutputStream via an XSLT stylesheet.
        Throws:
        java.io.IOException
      • getSAXReaderPoolCount

        public static int getSAXReaderPoolCount()
        Returns:
        the number of SAXReaders in the reader pool
      • mergeSchemaLocations

        public static void mergeSchemaLocations​(org.dom4j.Document src,
                                                org.dom4j.Document dest)
        Merge all the xsd schema loations from one doc to another.
      • changeNamespace

        public static void changeNamespace​(org.dom4j.Element el,
                                           org.dom4j.Namespace namespace)
        Traverse the element to the roots and updates the namespace
        Parameters:
        el -
        namespace -
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(org.xml.sax.InputSource xml,
                                                      boolean validate)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(java.io.InputStream xml)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(java.io.File xml)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(java.io.InputStream xml,
                                                      boolean validate)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(java.io.File xml,
                                                      boolean validate)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(java.lang.String xml)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • parseAsW3c

        public static org.w3c.dom.Document parseAsW3c​(java.lang.String xml,
                                                      boolean validate)
                                               throws org.xml.sax.SAXException,
                                                      java.io.IOException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
      • prettyPrint

        public static java.lang.String prettyPrint​(org.w3c.dom.Node n)
      • prettyPrint

        public static java.lang.String prettyPrint​(org.w3c.dom.Node n,
                                                   int indent)
      • selectSingleNode

        public static java.lang.Object selectSingleNode​(org.w3c.dom.Node n,
                                                        java.lang.String xpath)
                                                 throws org.jaxen.JaxenException
        Throws:
        org.jaxen.JaxenException
      • selectNodes

        public static java.util.List<?> selectNodes​(org.w3c.dom.Node n,
                                                    java.lang.String xpath)
                                             throws org.jaxen.JaxenException
        Throws:
        org.jaxen.JaxenException