Android XML String Format prettyPrint(String unformattedXml)

Here you can find the source of prettyPrint(String unformattedXml)

Description

pretty Print

Declaration

public static String prettyPrint(String unformattedXml) 

Method Source Code

//package com.java2s;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
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 prettyPrint(String unformattedXml) {
        try {//from   w  ww  .  j  a  v a 2 s.co m
            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(
                    "{http://xml.apache.org/xslt}indent-amount", "2");
            // initialize StreamResult with File object to save to file
            StreamResult result = new StreamResult(new StringWriter());
            StreamSource source = new StreamSource(new StringReader(
                    unformattedXml));
            transformer.transform(source, result);
            return result.getWriter().toString();
        } catch (Throwable t) {
            // should never happen
            throw new RuntimeException(t);
        }
    }
}

Related

  1. header()