Class XMLUtils


  • public final class XMLUtils
    extends Object
    Utilities useful when working with various XML types.
    Since:
    1.596.1 and 1.600, unrestricted since 2.179
    • Constructor Detail

      • XMLUtils

        public XMLUtils()
    • Method Detail

      • safeTransform

        public static void safeTransform​(@NonNull
                                         Source source,
                                         @NonNull
                                         Result out)
                                  throws TransformerException,
                                         SAXException
        Transform the source to the output in a manner that is protected against XXE attacks. If the transform can not be completed safely then an IOException is thrown. Note - to turn off safety set the system property disableXXEPrevention to true.
        Parameters:
        source - The XML input to transform. - This should be a StreamSource or a SAXSource in order to be able to prevent XXE attacks.
        out - The Result of transforming the source.
        Throws:
        TransformerException
        SAXException
      • parse

        @NonNull
        public static Document parse​(@NonNull
                                     InputStream stream)
                              throws SAXException,
                                     IOException
        Parse the supplied XML stream data to a Document.

        This function does not close the stream.

        Parameters:
        stream - The XML stream.
        Returns:
        The XML Document.
        Throws:
        SAXException - Error parsing the XML stream data e.g. badly formed XML.
        IOException - Error reading from the steam.
        Since:
        2.265
      • parse

        @NonNull
        public static Document parse​(@NonNull
                                     File file)
                              throws SAXException,
                                     IOException
        Parse the supplied XML file data to a Document.
        Parameters:
        file - The file to parse.
        Returns:
        The parsed document.
        Throws:
        SAXException - Error parsing the XML file data e.g. badly formed XML.
        IOException - Error reading from the file.
        Since:
        2.265
      • getValue

        @NonNull
        public static String getValue​(@NonNull
                                      String xpath,
                                      @NonNull
                                      File file)
                               throws IOException,
                                      SAXException,
                                      XPathExpressionException
        The a "value" from an XML file using XPath.

        Uses the system encoding for reading the file.

        Parameters:
        xpath - The XPath expression to select the value.
        file - The file to read.
        Returns:
        The data value. An empty String is returned when the expression does not evaluate to anything in the document.
        Throws:
        IOException - Error reading from the file.
        SAXException - Error parsing the XML file data e.g. badly formed XML.
        XPathExpressionException - Invalid XPath expression.
        Since:
        2.0
      • getValue

        @NonNull
        public static String getValue​(@NonNull
                                      String xpath,
                                      @NonNull
                                      File file,
                                      @NonNull
                                      String fileDataEncoding)
                               throws IOException,
                                      SAXException,
                                      XPathExpressionException
        The a "value" from an XML file using XPath.
        Parameters:
        xpath - The XPath expression to select the value.
        file - The file to read.
        fileDataEncoding - The file data format.
        Returns:
        The data value. An empty String is returned when the expression does not evaluate to anything in the document.
        Throws:
        IOException - Error reading from the file.
        SAXException - Error parsing the XML file data e.g. badly formed XML.
        XPathExpressionException - Invalid XPath expression.
        Since:
        2.0
      • getValue

        public static String getValue​(String xpath,
                                      Document document)
                               throws XPathExpressionException
        The a "value" from an XML file using XPath.
        Parameters:
        xpath - The XPath expression to select the value.
        document - The document from which the value is to be extracted.
        Returns:
        The data value. An empty String is returned when the expression does not evaluate to anything in the document.
        Throws:
        XPathExpressionException - Invalid XPath expression.
        Since:
        2.0