Class XpathXmlUpgrader


  • public class XpathXmlUpgrader
    extends Object
    Helper class to perform transformation of an XML file on nodes matching given XPath expression.
    • Constructor Detail

      • XpathXmlUpgrader

        public XpathXmlUpgrader()
    • Method Detail

      • visit

        public static void visit​(File file,
                                 String xPathExpression,
                                 Consumer<Node> visitor)
        Read XML from file, performing actions on each visited node matching xPathExpression. The visitor should not perform transformation on accepted nodes, as the modifications will not be persisted.
        Parameters:
        file - XML file to transform
        xPathExpression - nodes to be modified
        visitor - action to execute on each matching node
        See Also:
        transform(File, String, Consumer)
      • transform

        public static void transform​(File file,
                                     String xPathExpression,
                                     Consumer<Node> transformation)
        Read XML from file, apply transformation on nodes matching xPathExpression and write results back to the same file. Note: transformation is performed in-place, i.e. node is not being replaced by a new one.
        Parameters:
        file - XML file to transform
        xPathExpression - nodes to be modified
        transformation - how to modify nodes
        See Also:
        visit(File, String, Consumer)
      • visit

        public static Document visit​(Document document,
                                     String xPathExpression,
                                     Consumer<Node> visitor)
        Iterate over elements of document, performing actions on each visited node matching xPathExpression. The visitor may apply in-place transformation.
        Parameters:
        document - document transform
        xPathExpression - nodes to be modified
        visitor - action to execute on each matching node
      • read

        public static Optional<Document> read​(File inputFile)
        Read an XML document from a file.
        Parameters:
        inputFile - XML file
        Returns:
        Optional<Document>
      • read

        public static Optional<Document> read​(InputStream input)
        Read an XML document from an input stream.
        Parameters:
        input - input stream
        Returns:
        Optional<Document>
      • write

        public static void write​(Document dom,
                                 File outputFile)
        Write an XML document to a file.
        Parameters:
        dom - document
        outputFile - XML file
      • write

        public static void write​(Document dom,
                                 OutputStream output)
        Write an XML document to an output stream.
        Parameters:
        dom - document
        output - output stream