Java XML Format prettyFormat(String input, int indent)

Here you can find the source of prettyFormat(String input, int indent)

Description

pretty Format

License

Apache License

Declaration

public static String prettyFormat(String input, int indent) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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.*;

public class Main {
    public static String prettyFormat(String input, int indent) {
        try {/*from  ww w  .  java  2  s  . c om*/
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. formatXMLStr(String xml)
  2. prettifyString(String string)
  3. prettyFormat(String input)
  4. prettyFormat(String input)
  5. prettyFormat(String input)
  6. prettyFormat(String strInput, int nIndent)
  7. prettyFormatXml(final InputStream xml, final OutputStream os, final int indent)
  8. prettyFormatXmlText(String text)
  9. prettyPrint(final Source source)