Indent,Organize or Well-form XML Using Java

Below Code Well form, Organize or Indent plain or unorganized XML String using XML API in JDK 6.

IndentXML.java
import com.actl.dxchange.utilities.XMLUtil;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.*;

/**
 * XMLTransformations Utilities.
 * @author Tejas Purohit
 */
public class IndentXML {

 public IndentXML() {

 }

 public static void main(String[] args) throws Exception {

  IndentXML t = new IndentXML();

  String strXML = "<Employees><Employee Name='Amitabh Bachapan' Designation='Director'>"+
    "<Address><AddressLine>Opp. Taj Square</AddressLine><City>Mumbai</City>"+
    "<Country>India</Country></Address></Employee><Employee Name='Shah RukRukKhan'"+
    " Designation='Manager'><Address><AddressLine>Behind Chandani Choak</AddressLine>"+
    "<City>Delhi</City><Country>India</Country></Address></Employee></Employees>";

  System.out.println("Plain XML:");
  System.out.println(strXML); // Print Plain XML

  strXML = t.indentXML(strXML); // Indent XML

  System.out.println("Organized XML:"); // Print Organized/Indented XML
  System.out.println(strXML);

 }

 /**
  * Indent given XML String and returns organized String.
  * @param strXML Plain XML String
  * @return Organized or indented String
  */
 public String indentXML(String strXML) {

  String result = null;

  try {

   // Creating StreamSource from given plain XML String
   ByteArrayInputStream bin = new ByteArrayInputStream(strXML.getBytes());
   StreamSource streamSource = new StreamSource(bin);
   streamSource.setSystemId("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");

   // Creating new Transformer
   TransformerFactory transFactory = TransformerFactory.newInstance();
   Transformer transformer = transFactory.newTransformer();

   // Setting output properties
   transformer.setOutputProperty(OutputKeys.METHOD, "xml");
   transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
   transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
   transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
   transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
   transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD XHTML 1.1//EN");


   // Creating StreamResult
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   StreamResult streamResult = new StreamResult(bout);
   streamResult.setSystemId("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");

   // Transforming StreamSource and storing result in StreamResult
   transformer.transform(streamSource, streamResult);

   // Getting String from ByteArrayOutputStream
   result = bout.toString();

  } catch (Exception ex) {

   ex.printStackTrace();

  }

  return result;

 }

}

Plain XML output will be like:
<Employees><Employee Name='Amitabh Bachapan' Designation='Director'><Address><AddressLine>Opp. Taj Square</AddressLine><City>Mumbai</City><Country>India</Country></Address></Employee><Employee Name='Shah RukRukKhan' Designation='Manager'><Address><AddressLine>Behind Chandani Choak</AddressLine><City>Delhi</City><Country>India</Country></Address></Employee></Employees>

Well formed/Organized/Indented XML output will like:


<!DOCTYPE Employees PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<Employees>
 <Employee Name="Amitabh Bachapan" Designation="Director">
  <Address>
   <AddressLine>Opp. Taj Square</AddressLine>
   <City>Mumbai</City>
   <Country>India</Country>
  </Address>
 </Employee>
 <Employee Name="Shah RukRukKhan" Designation="Manager">
  <Address>
   <AddressLine>Behind Chandani Choak</AddressLine>
   <City>Delhi</City>
   <Country>India</Country>
  </Address>
 </Employee>
</Employees>

Comments