Class XmlUtils


  • public final class XmlUtils
    extends Object
    XMl utility methods.
    Author:
    Tom Fennelly
    • Field Detail

      • LT

        public static final char[] LT
      • GT

        public static final char[] GT
      • AMP

        public static final char[] AMP
      • QUOT

        public static final char[] QUOT
      • APOS

        public static final char[] APOS
    • Method Detail

      • removeEntities

        public static void removeEntities​(Reader reader,
                                          Writer writer)
                                   throws IOException
        Remove all entities from the supplied Reader stream replacing them with their actual character values.

        Both the read and write streams are returned unclosed.

        Parameters:
        reader - The read stream.
        writer - The write stream.
        Throws:
        IOException
      • removeEntities

        public static String removeEntities​(String string)
        Remove all entities from the supplied String stream replacing them with there actual character values.
        Parameters:
        string - The string on which the operation is to be carried out.
        Returns:
        The string with its entities rewriten.
      • rewriteEntities

        public static void rewriteEntities​(Reader reader,
                                           Writer writer)
                                    throws IOException
        Rewrite all entities from the supplied Reader stream replacing them with their character reference equivalents.

        Example:   is rewriten as  

        Both the read and write streams are returned unclosed.

        Parameters:
        reader - The read stream.
        writer - The write stream.
        Throws:
        IOException
      • getNodeList

        public static NodeList getNodeList​(Node node,
                                           String xpath)
        Get the W3C NodeList instance associated with the XPath selection supplied.
        Parameters:
        node - The document node to be searched.
        xpath - The XPath String to be used in the selection.
        Returns:
        The W3C NodeList instance at the specified location in the document, or null.
      • getNode

        public static Node getNode​(Node node,
                                   String xpath)
        Get the W3C Node instance associated with the XPath selection supplied.
        Parameters:
        node - The document node to be searched.
        xpath - The XPath String to be used in the selection.
        Returns:
        The W3C Node instance at the specified location in the document, or null.
      • getString

        public static String getString​(Node node,
                                       String xpath)
        Get the String data associated with the XPath selection supplied.
        Parameters:
        node - The node to be searched.
        xpath - The XPath String to be used in the selection.
        Returns:
        The string data located at the specified location in the document, or an empty string for an empty resultset query.
      • serialize

        public static String serialize​(NodeList nodeList,
                                       boolean closeEmptyElements)
                                throws DOMException
        Serialise the supplied W3C DOM subtree.

        The output is unformatted.

        Parameters:
        nodeList - The DOM subtree as a NodeList.
        Returns:
        The subtree in serailised form.
        Throws:
        DOMException - Unable to serialise the DOM.
      • serialize

        public static String serialize​(Node node,
                                       boolean format,
                                       boolean closeEmptyElements)
                                throws DOMException
        Serialise the supplied W3C DOM subtree.
        Parameters:
        node - The DOM node to be serialized.
        format - Format the output.
        Returns:
        The subtree in serailised form.
        Throws:
        DOMException - Unable to serialise the DOM.
      • serialize

        public static void serialize​(Node node,
                                     boolean format,
                                     Writer writer,
                                     boolean closeEmptyElements)
                              throws DOMException
        Serialise the supplied W3C DOM subtree.
        Parameters:
        node - The DOM node to be serialized.
        format - Format the output.
        writer - The target writer for serialization.
        Throws:
        DOMException - Unable to serialise the DOM.
      • serialize

        public static String serialize​(NodeList nodeList,
                                       boolean format,
                                       boolean closeEmptyElements)
                                throws DOMException
        Serialise the supplied W3C DOM subtree.
        Parameters:
        nodeList - The DOM subtree as a NodeList.
        format - Format the output.
        Returns:
        The subtree in serailised form.
        Throws:
        DOMException - Unable to serialise the DOM.
      • serialize

        public static void serialize​(NodeList nodeList,
                                     boolean format,
                                     Writer writer,
                                     boolean closeEmptyElements)
                              throws DOMException
        Serialise the supplied W3C DOM subtree.
        Parameters:
        nodeList - The DOM subtree as a NodeList.
        format - Format the output.
        writer - The target writer for serialization.
        Throws:
        DOMException - Unable to serialise the DOM.
      • indent

        public static String indent​(String xml,
                                    int indent)
        Indent the supplied XML string by the number of spaces specified in the 'indent' param.

        The indents are only inserted after newlines, where the first non-whitespace character is '<'.

        Parameters:
        xml - The XML to indent.
        indent - The number of spaces to insert as the indent.
        Returns:
        The indented XML string.
      • isTextNode

        public static boolean isTextNode​(Node node)
        Is the supplied W3C DOM Node a text node.
        Parameters:
        node - The node to be tested.
        Returns:
        True if the node is a text node, otherwise false.
      • encodeTextValue

        public static void encodeTextValue​(char[] characters,
                                           int offset,
                                           int length,
                                           Writer writer)
                                    throws IOException
        Throws:
        IOException
      • encodeAttributeValue

        public static void encodeAttributeValue​(char[] characters,
                                                int offset,
                                                int length,
                                                Writer writer)
                                         throws IOException
        Throws:
        IOException