Java XML Document to String document2String(Document doc, boolean prettyPrint)

Here you can find the source of document2String(Document doc, boolean prettyPrint)

Description

document String

License

Open Source License

Declaration

public static String document2String(Document doc, boolean prettyPrint)
            throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    public static String document2String(Document doc, boolean prettyPrint)
            throws Exception {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();/*from w w  w. j ava  2 s  .  c om*/

        if (prettyPrint) {
            transformer.setOutputProperty(
                    "{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        }

        transformer.transform(new DOMSource(doc), new StreamResult(writer));

        return writer.toString();
    }
}

Related

  1. document2String(Document doc)
  2. document2String(Node document)
  3. document2XmlString(Document xmldoc)
  4. documentToString(Document d)
  5. documentToString(Document d)