Java XML Format prettyXml(String xml)

Here you can find the source of prettyXml(String xml)

Description

pretty Xml

License

Apache License

Parameter

Parameter Description
xml a parameter

Return

pretty xml

Declaration

public static String prettyXml(String xml) 

Method Source Code

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

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

public class Main {
    /**//from  www  .j  a v  a  2 s  .  c  o m
     * Document builder factory
     */
    private static DocumentBuilderFactory factory = null;
    private static TransformerFactory transFactory = null;

    /**
     * @param xml
     * @return pretty xml
     */
    public static String prettyXml(String xml) {
        Transformer serializer;
        try {
            Source source = new StreamSource(new StringReader(xml));
            StringWriter writer = new StringWriter();

            serializer = transFactory.newTransformer();
            // Setup indenting to "pretty print"
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            serializer.transform(source, new StreamResult(writer));
            return writer.toString();
        } catch (Exception e) {
            return xml;
        }
    }

    public static Document transform(Document dom, Document xslt) {
        try {
            DOMSource xsltSource = new DOMSource(xslt);
            Transformer transformer = transFactory.newTransformer(xsltSource);
            DOMSource source = new DOMSource(dom);
            Document out = newDocument();
            DOMResult result = new DOMResult(out);
            transformer.transform(source, result);
            return out;
        } catch (Exception e) {
            throw new RuntimeException("Could not write dom tree '" + dom.getBaseURI() + "'!", e);
        }
    }

    /**
     * Creates a new Document using the default XML implementation
     * 
     * @return DOM
     */
    public static Document newDocument() {
        try {
            return factory.newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

  1. prettyPrintXml(String input)
  2. prettyPrintXML(String xml)
  3. prettyXml(final String xml)
  4. prettyXml(String xml)
  5. prettyXml(String xml)
  6. prettyXmlPrint(Node xml, OutputStream out)
  7. print(Element elm, OutputStream out)
  8. print(File file)
  9. print(Node node)