Java tutorial
//package com.java2s; import java.io.StringReader; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Node; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; import org.xml.sax.InputSource; public class Main { public static String formatXml(String xml) { try { final InputSource src = new InputSource(new StringReader(xml)); final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); //May need this: System.setProperty(DOMImplementationRegistry. //PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the declaration is needed to be outputted. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); return writer.writeToString(document); } catch (Exception e) { System.err.println("Exception thrown"); throw new RuntimeException(e); } } }