XmlPrettyPrinter { @NonNull public static String prettyPrint( @NonNull String xml, @NonNull XmlFormatPreferences prefs, @NonNull XmlFormatStyle style, @Nullable String lineSeparator) { Document document = XmlUtils.parseDocumentSilently(xml, true); if (document != null) { XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, lineSeparator); printer.setEndWithNewline(xml.endsWith(printer.getLineSeparator())); StringBuilder sb = new StringBuilder(3 * xml.length() / 2); printer.prettyPrint(-1, document, null, null, sb, false ); return sb.toString(); } else { return xml; } } XmlPrettyPrinter(XmlFormatPreferences prefs, XmlFormatStyle style,
            String lineSeparator); XmlPrettyPrinter setEndWithNewline(boolean endWithNewline); void setIndentationLevels(String[] indentationLevels); @NonNull static String prettyPrint(
            @NonNull String xml,
            @NonNull XmlFormatPreferences prefs,
            @NonNull XmlFormatStyle style,
            @Nullable String lineSeparator); @NonNull @Deprecated static String prettyPrint(
            @NonNull Node node,
            @NonNull XmlFormatPreferences prefs,
            @NonNull XmlFormatStyle style,
            @Nullable String lineSeparator); @NonNull static String prettyPrint(
            @NonNull Node node,
            @NonNull XmlFormatPreferences prefs,
            @NonNull XmlFormatStyle style,
            @Nullable String lineSeparator,
            boolean endWithNewline); @NonNull @Deprecated static String prettyPrint(@NonNull Node node); @NonNull static String prettyPrint(@NonNull Node node, boolean endWithNewline); void prettyPrint(int rootDepth, Node root, Node startNode, Node endNode,
            StringBuilder out, boolean openTagOnly); static void main(String[] args);  }
@Test public void testToXml() throws Exception { Document doc = createEmptyPlainDocument(); assertNotNull(doc); Element root = doc.createElement("myroot"); doc.appendChild(root); root.setAttribute("foo", "bar"); root.setAttribute("baz", "baz"); Element child = doc.createElement("mychild"); root.appendChild(child); Element child2 = doc.createElement("hasComment"); root.appendChild(child2); Node comment = doc.createComment("This is my comment"); child2.appendChild(comment); Element child3 = doc.createElement("hasText"); root.appendChild(child3); Node text = doc.createTextNode(" This is my text "); child3.appendChild(text); String xml = XmlUtils.toXml(doc); assertEquals( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<myroot baz=\"baz\" foo=\"bar\"><mychild/><hasComment><!--This is my comment--></hasComment><hasText> This is my text </hasText></myroot>", xml); xml = XmlPrettyPrinter.prettyPrint(doc, false); assertEquals("" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<myroot\n" + " baz=\"baz\"\n" + " foo=\"bar\" >\n" + "\n" + " <mychild />\n" + "\n" + " <hasComment> <!-- This is my comment -->\n" + " </hasComment>\n" + "\n" + " <hasText>\n" + " This is my text \n" + " </hasText>\n" + "\n" + "</myroot>", xml); }
@Test public void testToXml2() throws Exception { String xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <string \n" + " name=\"description_search\">Search</string>\n" + " <string \n" + " name=\"description_map\">Map</string>\n" + " <string\n" + " name=\"description_refresh\">Refresh</string>\n" + " <string \n" + " name=\"description_share\">Share</string>\n" + "</resources>"; Document doc = parse(xml); assertNotNull(doc); String formatted = XmlUtils.toXml(doc); assertEquals("" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <string name=\"description_search\">Search</string>\n" + " <string name=\"description_map\">Map</string>\n" + " <string name=\"description_refresh\">Refresh</string>\n" + " <string name=\"description_share\">Share</string>\n" + "</resources>", formatted); formatted = XmlPrettyPrinter.prettyPrint(doc, false); assertEquals("" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "\n" + " <string name=\"description_search\">Search</string>\n" + " <string name=\"description_map\">Map</string>\n" + " <string name=\"description_refresh\">Refresh</string>\n" + " <string name=\"description_share\">Share</string>\n" + "\n" + "</resources>", formatted); }
@Test public void testToXml3() throws Exception { String xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<root>\n" + " <!-- ============== -->\n" + " <!-- Generic styles -->\n" + " <!-- ============== -->\n" + "</root>"; Document doc = parse(xml); assertNotNull(doc); String formatted = XmlUtils.toXml(doc); assertEquals(xml, formatted); xml = XmlPrettyPrinter.prettyPrint(doc, false); assertEquals("" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<root>\n" + "\n" + " <!-- ============== -->\n" + " <!-- Generic styles -->\n" + " <!-- ============== -->\n" + "\n" + "</root>", xml); }
@Test public void testToXml3b() throws Exception { String xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <!-- ============== -->\n" + " <!-- Generic styles -->\n" + " <!-- ============== -->\n" + " <string name=\"test\">test</string>\n" + "</resources>"; Document doc = parse(xml); assertNotNull(doc); String formatted = XmlUtils.toXml(doc); assertEquals("" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <!-- ============== -->\n" + " <!-- Generic styles -->\n" + " <!-- ============== -->\n" + " <string name=\"test\">test</string>\n" + "</resources>", formatted); xml = XmlPrettyPrinter.prettyPrint(doc, false); assertEquals("" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "\n" + " <!-- ============== -->\n" + " <!-- Generic styles -->\n" + " <!-- ============== -->\n" + " <string name=\"test\">test</string>\n" + "\n" + "</resources>", xml); }