XmlUtil { public static String domToString(Node node, String encoding, boolean pretty) throws ProblemException { if (node == null) return null; String usedEncoding = encoding == null ? DEFAULT_ENCODING : encoding; if (Checker.isBlank(usedEncoding)) { throw new ProblemException(XmlProblem.INVALID_ENCODING); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); DOMSource source = new DOMSource(node); try (StringWriter writer = new StringWriter()) { StreamResult result = new StreamResult(writer); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, usedEncoding); transformer.setOutputProperty(OutputKeys.INDENT, pretty ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.STANDALONE, "no"); transformer.transform(source, result); return writer.toString(); } catch (Exception e) { logger.error(e.toString(), e); throw new ProblemException(e, XmlProblem.SERIALIZE_DOM); } }  static Document createDom(); static Document createDOMDocument(String xml); static String prettyFormatXml(String xml, String encoding); static String domToString(Node node, String encoding, boolean pretty); static Node findNode(Node document, String xPathExpression); static Element getFirstElement(Node node); static boolean containsElements(NodeList nodeList); static XMLGregorianCalendar newXMLGregorianCalendar(Date date); static T unmarshal(Class<T> targetClass, String xml); static String marshal(T object); static Source getSource(String xml);  }
@Test public void testPrettyFormatXmlDocumentString() throws Exception { final String EXPECTED = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<root>\n<elem attr1=\"value1\" b-attr1=\"value2\">content</elem>\n</root>\n"; String actual = XmlUtil.domToString(DOCUMENT, ENCODING, true); actual = actual.replace("\r\n", "\n"); assertEquals(EXPECTED, actual); assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>", XmlUtil.domToString(new DocumentImpl(), null, false)); }