Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

XMLBeautifier

By: a guest on Jul 16th, 2012  |  syntax: Java  |  size: 2.71 KB  |  views: 591  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.ByteArrayInputStream;
  2.  
  3. import javax.xml.parsers.DocumentBuilderFactory;
  4.  
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.bootstrap.DOMImplementationRegistry;
  7. import org.w3c.dom.ls.DOMImplementationLS;
  8. import org.w3c.dom.ls.LSSerializer;
  9.  
  10. public class XMLBeautifier {
  11.    Node document;
  12.    boolean keepDeclaration;
  13.    
  14.    public XMLBeautifier(String xml) {
  15.       try {
  16.          this.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();
  17.          this.keepDeclaration = xml.startsWith("<?xml");
  18.       } catch (Exception e) {
  19.          e.printStackTrace();
  20.       }
  21.    }
  22.    
  23.    public String getXML() {
  24.       try {
  25.          DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  26.          DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
  27.          LSSerializer writer = impl.createLSSerializer();
  28.          
  29.          writer.getDomConfig().setParameter("format-pretty-print", true); // Set this to true if the output needs to be beautified.
  30.          writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
  31.          
  32.          return writer.writeToString(document);
  33.       } catch (Exception e) {
  34.          e.printStackTrace();
  35.          return null;
  36.       }
  37.    }
  38.    
  39.    public static void main(String[] args) {
  40.       String xml = "<shiporder orderid=\"889923\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"shiporder.xsd\"><orderperson>John Smith</orderperson><shipto><name>Ola Nordmann</name><address>Langgt 23</address><city>4000 Stavanger</city><country>Norway</country></shipto><item><title>Empire Burlesque</title><note>Special Edition</note><quantity>1</quantity><price>10.90</price></item><item><title>Hide your heart</title><quantity>1</quantity><price>9.90</price></item></shiporder>";
  41.      
  42.       System.out.println(new XMLBeautifier(xml).getXML());
  43.    }
  44. }
  45.  
  46. ------------------------ CONSOLE -------------------------
  47. <shiporder orderid="889923" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd">
  48.     <orderperson>John Smith</orderperson>
  49.     <shipto>
  50.         <name>Ola Nordmann</name>
  51.         <address>Langgt 23</address>
  52.         <city>4000 Stavanger</city>
  53.         <country>Norway</country>
  54.     </shipto>
  55.     <item>
  56.         <title>Empire Burlesque</title>
  57.         <note>Special Edition</note>
  58.         <quantity>1</quantity>
  59.         <price>10.90</price>
  60.     </item>
  61.     <item>
  62.         <title>Hide your heart</title>
  63.         <quantity>1</quantity>
  64.         <price>9.90</price>
  65.     </item>
  66. </shiporder>