Java tutorial
//package com.java2s; import java.io.StringReader; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class Main { public static String getFormattedXml(String xmlString) { // /////////////////////////////////////////////////////////////// // Declarations // /////////////////////////////////////////////////////////////// Source xmlInput = null; StringWriter stringWriter = null; StreamResult xmlOutput = null; TransformerFactory transformerFactory = null; Transformer transformer = null; String formattedXml = null; // /////////////////////////////////////////////////////////////// // Code // /////////////////////////////////////////////////////////////// try { xmlInput = new StreamSource(new StringReader(xmlString)); stringWriter = new StringWriter(); xmlOutput = new StreamResult(stringWriter); transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); formattedXml = xmlOutput.getWriter().toString(); } catch (Exception e) { // To Do: Handle Exception.. } return formattedXml; } }