Skip to content

Instantly share code, notes, and snippets.

@sachin-handiekar
Created October 16, 2011 20:37
Show Gist options
  • Save sachin-handiekar/1291393 to your computer and use it in GitHub Desktop.
Save sachin-handiekar/1291393 to your computer and use it in GitHub Desktop.
XML Formatter/Pretty Printer Java
import org.xml.sax.InputSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class PrettyPrinter {
public static String prettyPrintXml(String sourceXml) {
try {
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
// serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// serializer.setOutputProperty("{http://xml.customer.org/xslt}indent-amount", "2");
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(sourceXml.getBytes())));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
} catch (Exception e) {
// TODO log error
return sourceXml;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment