public static String formatXml(String xml){
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            Transformer serializer= factory.newTransformer();
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            Source xmlSource = new SAXSource(new InputSource(new StringReader(xml)));
            StringWriter stringWriter = new StringWriter();
            StreamResult res = new StreamResult(stringWriter);
            serializer.transform(xmlSource, res);
            return stringWriter.toString();
        } catch (Exception e) {
            return xml;
        }
    }
@Test
    public void testFormatXmlInvalid() {
        PrintStream origErr = System.err;
        try {
            // The parser will print an error, so let it go where we will not see it
            System.setErr(new PrintStream(new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    // ignore output
                }
            }));
            assertThat("No well formed xml here", CoreMatchers
                    .is(XPathUtil.formatXml("No well formed xml here")));
        } finally {
            System.setErr(origErr);
        }
    }