Java tutorial
//package com.java2s; 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; import org.xml.sax.InputSource; public class Main { public static String prettyFormat(String xml) { if (xml == null || xml.isEmpty() || !xml.contains("<")) { // System.out.println("Why?"+xml.startsWith("<", 0)); return xml; } 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"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).replace("><", ">\n<"); } catch (Exception e) { System.out.println("prettyFormat: Error.." + e.getMessage()); //TODO log error return xml.replace("<", "\n<"); // return xml.replace("><", ">\n<"); } } }