XML Pretty Printer in Java

Here is a sample program in Java, how to format an XML to pretty format.

 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;  
 /**  
  *  
  * @author lananda  
  */  
 public class PrettyXmlWriter {  
    public static void main(String... args){  
     String unformattedXml  
         = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>"  
         + "<QueryMessage\n"  
         + "    xmlns=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message\"\n"  
         + "    xmlns:query=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/query\">\n"  
         + "  <Query>\n"  
         + "    <query:CategorySchemeWhere>\n"  
         + "  \t\t\t\t\t     <query:AgencyID>ECB\n\n\n\n</query:AgencyID>\n"  
         + "    </query:CategorySchemeWhere>\n"  
         + "  </Query>\n\n\n\n\n"  
         + "</QueryMessage>";  
     unformattedXml = unformattedXml.replaceAll("\\s+", " ");  
     String format = format(unformattedXml);  
     System.out.println(format);  
   }  
     public static String format(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();  
       writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.  
       writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.  
       return writer.writeToString(document);  
     } catch (Exception e) {  
       throw new RuntimeException(e);  
     }  
   }  
 }  

Comments