Advertisement
Guest User

XMLBeautifier

a guest
Jul 16th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  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.    
  13.    public XMLBeautifier(String xml) {
  14.       try {
  15.          this.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();
  16.       } catch (Exception e) {
  17.          e.printStackTrace();
  18.       }
  19.    }
  20.    
  21.    public String getXML() {
  22.       try {
  23.          DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  24.          DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
  25.          LSSerializer writer = impl.createLSSerializer();
  26.          
  27.          writer.getDomConfig().setParameter("format-pretty-print", true); // Set this to true if the output needs to be beautified.
  28.          writer.getDomConfig().setParameter("xml-declaration", false); // Set this to true if the declaration is needed to be outputted.
  29.          
  30.          return writer.writeToString(document);
  31.       } catch (Exception e) {
  32.          e.printStackTrace();
  33.          return null;
  34.       }
  35.    }
  36.    
  37.    public static void main(String[] args) {
  38.       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>";
  39.      
  40.       System.out.println(new XMLBeautifier(xml).getXML());
  41.    }
  42. }
  43.  
  44. ------------------------ CONSOLE -------------------------
  45. <shiporder orderid="889923" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd">
  46.     <orderperson>John Smith</orderperson>
  47.     <shipto>
  48.         <name>Ola Nordmann</name>
  49.         <address>Langgt 23</address>
  50.         <city>4000 Stavanger</city>
  51.         <country>Norway</country>
  52.     </shipto>
  53.     <item>
  54.         <title>Empire Burlesque</title>
  55.         <note>Special Edition</note>
  56.         <quantity>1</quantity>
  57.         <price>10.90</price>
  58.     </item>
  59.     <item>
  60.         <title>Hide your heart</title>
  61.         <quantity>1</quantity>
  62.         <price>9.90</price>
  63.     </item>
  64. </shiporder>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement