Class XMLUtil


  • public class XMLUtil
    extends Object
    Various xml manipulation methods.
    • Method Detail

      • getChildContents

        public static String getChildContents​(Node node,
                                              String childName)
        Return the text representation of the first child of the node with the specified name.
        Parameters:
        node - The parent node
        childName - The name of the child of which the text should be returned.
        Returns:
        The first child node represetation with the specified name or null if such a child could not be found.
      • getChildNode

        public static Node getChildNode​(Node parent,
                                        String childName)
        Return the first child of the node with the specified name.
        Parameters:
        node - The parent node
        childName - The name of the child that should be returned.
        Returns:
        The first child node with the specified name or null if such a child could not be found.
      • getChildNodes

        public static Node[] getChildNodes​(Node parent,
                                           String childName)
        Return the children of the node with the specified name.
        Parameters:
        node - The parent node
        childName - The name of the children that should be returned.
        Returns:
        The child nodes with the specified name.
      • escapeToXML

        public static String escapeToXML​(String value)
        Escapes special xml characters.
        Parameters:
        value - The string that should be escaped.
        Returns:
        The escapes xml representation of the value value argument.
      • getAttribute

        public static String getAttribute​(Node node,
                                          String attributeName)
        Return the attribute with a specified name from a node.
        Parameters:
        node - The parent node of which the attribute should be returned.
        attributeName - The name of the attribute that should be matched.
        Returns:
        The string contents of the matching attribute, or null if the attribute could not be found
      • getTextContents

        public static String getTextContents​(Node node)
        Extracts the text content of a node.
        Parameters:
        node - The node of which the text representation should be returned.
        Returns:
        The text representation of the node or null if the argument is invalid.
      • normalize

        public static String normalize​(String xml)
                                throws SAXException
        Normalizes an xml string.
        Parameters:
        xml - The xml that should be normalized
        Returns:
        The normalized equivalent of the xml.
        Throws:
        SAXException - if the source could not be parsed.
      • node2Xml

        public static String node2Xml​(Node node)
                               throws TransformerException
        Converts a node to xml.
        Parameters:
        node - The node for which the xml representation should be returned.
        Returns:
        The xml source representation of the node.
        Throws:
        TransformerException - if the contents of the node could not be normalized.
      • doc2Xml

        public static String doc2Xml​(Document doc)
                              throws TransformerException
        Converts a document to xml.
        Parameters:
        node - The document for which the xml representation should be returned.
        Returns:
        The xml source representation of the document.
        Throws:
        TransformerException - if the contents of the document could not be normalized.
      • transform

        public static String transform​(String stylesheet,
                                       String xmlData)
                                throws TransformerException
        Transform xml with a stylesheet and return the result. The transformer is obtained from a pool so if you do multiple transformations with the same stylesheet, the transformer is reused from a pool.
        Parameters:
        stylesheet - the xsl stylesheet
        xmlData - the xml
        Returns:
        result of transformation.
        Throws:
        TransformerException - if something goes wrong during the transformation
      • transform

        public static void transform​(String stylesheet,
                                     Reader in,
                                     Writer out)
                              throws TransformerException
        Transforms xml from the input reader and writes the result to the writer.
        Parameters:
        stylesheet - the xsl stylesheet
        in - the xml input reader
        out - the writer for the transformation result
        Throws:
        TransformerException - if something goes wrong during the transformation
      • main

        public static void main​(String[] args)
        Main method that reads an xsl and xml file and returns the transformation result.
        Parameters:
        args - The file arguments
      • getFileContents

        public static String getFileContents​(String fileName)
                                      throws IOException
        Extracts the contents of a file.
        Parameters:
        fileName - The absolute path to the file.
        Returns:
        The string contents of the file.
        Throws:
        IOException - if the file could not be read.