I have an xml which is non-format as below. How i can make it nicely formatted xml.
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <catalog><book id="bk101"><author>Amuthan G</author><title>Spring MVC: Beginner's Guide</title><genre>Computer</genre> <price>40.49</price><publish_date>2014-07-25</publish_date><description>Your ultimate guide to building a complete web application using all the capabilities of Spring MVC</description></book><book id="bk102"><author> J. Sharma, Ashish Sarin</author><title>Getting started with Spring Framework</title><genre>Computer</genre><price>14.01</price> <publish_date>2012-12-10</publish_date><description>Getting started with Spring Framework is a hands-on guide to begin developing applications using Spring Framework.</description></book><book id="bk103"><author>Herbert Schildt</author><title>Java, A Beginner's Guide</title><genre>Computer</genre><price>37.75</price><publish_date>2011-08-16</publish_date><description>Learn the fundamentals of Java programming in no time from bestselling programming author Herb Schildt.</description></book></catalog> |
Pass the books.xml in the below program , run the program and see output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import org.w3c.dom.Document; import org.xml.sax.SAXException; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; public class XMLPrettyPrint { public static void main(String[] args) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; builder = factory.newDocumentBuilder(); Document doc = builder.parse(new FileInputStream (new File("C://Users/Sreenath/Desktop/books.xml"))); String str = xmlPrettyPrint(doc,"UTF-8"); System.out.println(str); } public static String xmlPrettyPrint(Document doc, String encoding) { String xmlString = null; try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); ByteArrayOutputStream xmlBytes = new ByteArrayOutputStream(); Transformer transformer = transformerFactory.newTransformer(); if(encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(doc), new StreamResult(xmlBytes)); xmlString = xmlBytes.toString(); } catch (Exception e) { e.printStackTrace(); } return xmlString; } } |
Output: Now we can see that books.xml is printed in nice format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <catalog> <book id="bk101"> <author>Amuthan G</author> <title>Spring MVC: Beginner's Guide</title> <genre>Computer</genre> <price>40.49</price> <publish_date>2014-07-25</publish_date> <description>Your ultimate guide to building a complete web application using all the capabilities of Spring MVC</description> </book> <book id="bk102"> <author> J. Sharma, Ashish Sarin</author> <title>Getting started with Spring Framework</title> <genre>Computer</genre> <price>14.01</price> <publish_date>2012-12-10</publish_date> <description>Getting started with Spring Framework is a hands-on guide to begin developing applications using Spring Framework.</description> </book> <book id="bk103"> <author>Herbert Schildt</author> <title>Java, A Beginner's Guide</title> <genre>Computer</genre> <price>37.75</price> <publish_date>2011-08-16</publish_date> <description>Learn the fundamentals of Java programming in no time from bestselling programming author Herb Schildt.</description> </book> </catalog> |