Pretty print XML with Java

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on December 28, 2015 · 1 min read

I have the following XML document all, in a single line:

3SUPERVISORRamu42SUPERVISORShyam45SUPERVISORRadhe38SUPERVISORJesse44SUPERVISORTECHNICIANSadhu22TECHNICIANYadav25TECHNICIANChris28TECHNICIANBill34TECHNICIANMANAGERSachin35MANAGERHamid32MANAGERNaresh34MANAGERManna36MANAGER

All I need is, to pretty print it with nice tabs and all. This is the code:

import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XmlPrettyPrinter {
    public void transform(InputStream unFormattedXmlInput, OutputStream prettyXmlOutput) throws TransformerException {
	TransformerFactory transformerFactory = TransformerFactory.newInstance();
	Transformer transformer = transformerFactory.newTransformer();
	transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
	transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
	transformer.setOutputProperty(OutputKeys.METHOD, "xml");
	transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
	transformer.transform(new StreamSource(unFormattedXmlInput), new StreamResult(prettyXmlOutput));
    }
}

This would nicely format it as below:


    3
    
        
            SUPERVISOR
            
                
                    Ramu
                    42
                    SUPERVISOR
                
                
                    Shyam
                    45
                    SUPERVISOR
                
                
                    Radhe
                    38
                    SUPERVISOR
                
                
                    Jesse
                    44
                    SUPERVISOR
                
            
        
        
            TECHNICIAN
            
                
                    Sadhu
                    22
                    TECHNICIAN
                
                
                    Yadav
                    25
                    TECHNICIAN
                
                
                    Chris
                    28
                    TECHNICIAN
                
                
                    Bill
                    34
                    TECHNICIAN
                
            
        
        
            MANAGER
            
                
                    Sachin
                    35
                    MANAGER
                
                
                    Hamid
                    32
                    MANAGER
                
                
                    Naresh
                    34
                    MANAGER
                
                
                    Manna
                    36
                    MANAGER
                
            
        
    

However, please note that it fails if there are spaces within the tags.
The source can be found here: https://github.com/paawak/blog/tree/master/code/pretty-print-xml