@Test public void format() { assertEquals( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<root>\n" + " <foo id=\"bar\"/>\n" + "</root>\n", XmlFormatter.format("<root><foo id=\"bar\"/></root>")); }
public static String format(final String in) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); final DocumentBuilder db = dbf.newDocumentBuilder(); final InputSource is = new InputSource(new StringReader(in)); final Document document = db.parse(is); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0")); if (impl == null) { return in; } final LSSerializer serializer = impl.createLSSerializer(); if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) { serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); final LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(document, lsOutput); return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<"); } return in; } catch (final Throwable t) { return in; } }
XmlFormatter { public static String format(final String in) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); final DocumentBuilder db = dbf.newDocumentBuilder(); final InputSource is = new InputSource(new StringReader(in)); final Document document = db.parse(is); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0")); if (impl == null) { return in; } final LSSerializer serializer = impl.createLSSerializer(); if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) { serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); final LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(document, lsOutput); return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<"); } return in; } catch (final Throwable t) { return in; } } }
XmlFormatter { public static String format(final String in) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); final DocumentBuilder db = dbf.newDocumentBuilder(); final InputSource is = new InputSource(new StringReader(in)); final Document document = db.parse(is); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0")); if (impl == null) { return in; } final LSSerializer serializer = impl.createLSSerializer(); if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) { serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); final LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(document, lsOutput); return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<"); } return in; } catch (final Throwable t) { return in; } } private  XmlFormatter(); }
XmlFormatter { public static String format(final String in) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); final DocumentBuilder db = dbf.newDocumentBuilder(); final InputSource is = new InputSource(new StringReader(in)); final Document document = db.parse(is); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0")); if (impl == null) { return in; } final LSSerializer serializer = impl.createLSSerializer(); if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) { serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); final LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(document, lsOutput); return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<"); } return in; } catch (final Throwable t) { return in; } } private  XmlFormatter(); static String format(final String in); }
XmlFormatter { public static String format(final String in) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); final DocumentBuilder db = dbf.newDocumentBuilder(); final InputSource is = new InputSource(new StringReader(in)); final Document document = db.parse(is); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0")); if (impl == null) { return in; } final LSSerializer serializer = impl.createLSSerializer(); if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) { serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); final LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(document, lsOutput); return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<"); } return in; } catch (final Throwable t) { return in; } } private  XmlFormatter(); static String format(final String in);  }