Here you can find the source of writeByTransformer(Node node, Writer writer, int indent)
private static void writeByTransformer(Node node, Writer writer, int indent) throws IOException
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Document; import org.w3c.dom.Node; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.IOException; import java.io.Writer; public class Main { private static void writeByTransformer(Node node, Writer writer, int indent) throws IOException { if (node.getNodeType() == Node.DOCUMENT_NODE) { ((Document) node).setXmlStandalone(true); }// w w w. ja v a2 s .c o m try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, indent == 0 ? "no" : "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); if (indent > 0) { transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", Integer.toString(indent)); } transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n"); transformer.transform(new DOMSource(node), new StreamResult(writer)); } catch (TransformerConfigurationException e) { throw new IllegalStateException(e); } catch (TransformerException e) { throw new IllegalStateException(e); } } }