public String format(String xml) {
        try {
            boolean keepDeclaration = DECL_PATTERN.matcher(xml).find();
            if (trimElements) {
                xml = trimElements(xml);
            }
            Source xmlInput = new StreamSource(new StringReader(xml));
            StreamResult xmlOutput = new StreamResult(new StringWriter());
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, keepDeclaration? "no" : "yes");
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
public String format ( String xml ) { try { boolean keepDeclaration = DECL_PATTERN . matcher ( xml ) . find ( ) ; if ( trimElements ) { xml = trimElements ( xml ) ; } Source xmlInput = new StreamSource ( new StringReader ( xml ) ) ; StreamResult xmlOutput = new StreamResult ( new StringWriter ( ) ) ; TransformerFactory transformerFactory = TransformerFactory . newInstance ( ) ; Transformer transformer = transformerFactory . newTransformer ( ) ; transformer . setOutputProperty ( "{http://xml.apache.org/xslt}indent-amount" , "4" ) ; transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; transformer . setOutputProperty ( OutputKeys . OMIT_XML_DECLARATION , keepDeclaration ? "no" : "yes" ) ; transformer . transform ( xmlInput , xmlOutput ) ; return xmlOutput . getWriter ( ) . toString ( ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
Creates formatted version of the supplied XML.
@param xml XML to format.
@return formatted version.