Monday, 26 March 2018

Pretty print xml string in java

In this post, I am going to show you how can we convert the xml string in pretty print format.

Input
<?xml version=\"1.0\"?><!-- Represents Books information in store --><books><book id=\"1\"><name>Let Us C</name><author>Yashwant Kanetkar</author><price>245.00</price></book><book id=\"2\"><name>Let Us C++</name><author>Yashwant Kanetkar</author><price>252.00</price></book><book id=\"3\"><name>Java The Complete Reference</name><author>Herbert Schildt</author><price>489.00</price></book><book id=\"4\"><name>HTML5 Black Book</name><author>Kogent Learning Solutions</author><price>485.00</price></book></books>"
                 
XMLUtil.java
package com.sample.util;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XMLUtil {

 public static String getPrettyString(String xmlData, int indent) throws Exception {
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  transformerFactory.setAttribute("indent-number", indent);

  Transformer transformer = transformerFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

  StringWriter stringWriter = new StringWriter();
  StreamResult xmlOutput = new StreamResult(stringWriter);

  Source xmlInput = new StreamSource(new StringReader(xmlData));
  transformer.transform(xmlInput, xmlOutput);

  return xmlOutput.getWriter().toString();
 }
}

Test.java
package com.sample.util;

public class Test {

 public static void main(String args[]) throws Exception {
  String xmlData = "<?xml version=\"1.0\"?><!-- Represents Books information in store --><books><book id=\"1\"><name>Let Us C</name><author>Yashwant Kanetkar</author><price>245.00</price></book><book id=\"2\"><name>Let Us C++</name><author>Yashwant Kanetkar</author><price>252.00</price></book><book id=\"3\"><name>Java The Complete Reference</name><author>Herbert Schildt</author><price>489.00</price></book><book id=\"4\"><name>HTML5 Black Book</name><author>Kogent Learning Solutions</author><price>485.00</price></book></books>";
  
  String str = XMLUtil.getPrettyString(xmlData, 2);
  
  System.out.println(str);
 }
}


Output
<?xml version="1.0" encoding="UTF-8"?><!-- Represents Books information in store --><books>
  <book id="1">
    <name>Let Us C</name>
    <author>Yashwant Kanetkar</author>
    <price>245.00</price>
  </book>
  <book id="2">
    <name>Let Us C++</name>
    <author>Yashwant Kanetkar</author>
    <price>252.00</price>
  </book>
  <book id="3">
    <name>Java The Complete Reference</name>
    <author>Herbert Schildt</author>
    <price>489.00</price>
  </book>
  <book id="4">
    <name>HTML5 Black Book</name>
    <author>Kogent Learning Solutions</author>
    <price>485.00</price>
  </book>
</books>

You may like



No comments:

Post a Comment