Friday, December 2, 2011

Indenting XML Output in Java

Here is a simple JAVA program for Indenting / Formatting a long XML string. You can provide a XML as a string OR can provide XML from a file according to your requirement.

Formatting an XML can be useful for reading or just simply pretty printing it.

Here is a complete tested code you can directly run the program and see the output.

/* IndentXML.java */

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class IndentXML {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException
    {
       
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       
        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
       
        //providing XML string to be indented
       
String xmlStringToIndent = "<AllEmployee><Employee><Name>Ram</Name><Designation>Programmer</Designation><Department>Development</Department></Employee><Employee><Name>Shyam</Name><Designation>TL</Designation><Department>Finanace</Department></Employee><Employee><Name>Geeta</Name><Designation>Manager</Designation><Department>HR</Department></Employee></AllEmployee>";
       
        //converting String to InputStream
        InputStream inStr = new ByteArrayInputStream(xmlStringToIndent.getBytes());
       
        Document doc = db.parse(inStr);
       
        //Output file
        File outputFile = new File("c:/indentedOutputXmlFile.xml");
        OutputStream outSrc = new FileOutputStream(outputFile);
       
        //creating a transformer
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer  = transFactory.newTransformer();
       
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
       
        transformer.transform(new DOMSource(doc), new StreamResult(outSrc));
       
        System.out.println("OutputFile created sucessfully!! "+outputFile.getPath());

    }

}


Input XML

String xmlStringToIndent = "<AllEmployee><Employee><Name>Ram</Name><Designation>Programmer</Designation><Department>Development</Department></Employee><Employee><Name>Shyam</Name><Designation>TL</Designation><Department>Finanace</Department></Employee><Employee><Name>Geeta</Name><Designation>Manager</Designation><Department>HR</Department></Employee></AllEmployee>";


Output XML

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<AllEmployee>
     <Employee>
          <Name>Ram</Name>
          <Designation>Programmer</Designation>
          <Department>Development</Department>
     </Employee>
     <Employee>
          <Name>Shyam</Name>
          <Designation>TL</Designation>
          <Department>Finanace</Department>
     </Employee>
     <Employee>
          <Name>Geeta</Name>
          <Designation>Manager</Designation>
          <Department>HR</Department>
     </Employee>
</AllEmployee>



 Note :

If you are passing XML string and it contains double quotes ("). Make to to bypass it by adding backward slash (\").

I observer below mentioned error when the string contained space in the beginning as show below.

String xmlStringToIndent = " <AllEmployee><Employee><Name>Ram</Name><Designation>Programmer ....."

Note the space in the beginning of the string. Make sure your XML is formatted properly.

Error

[Fatal Error] :1:7: The processing instruction target matching "[xX][mM][lL]" is not allowed.
Exception in thread "main" org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
    at com.hewitt.hre.fadv.IndentXML.main(IndentXML.java:43)