@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); }
@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 { @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 { @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 { @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); }
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);  }