Here you can find the source of saveToFile(String filename, Document document)
public static File saveToFile(String filename, Document document) throws TransformerException
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; public class Main { public static File saveToFile(String filename, Document document) throws TransformerException { // Prepare the DOM document for writing Source source = new DOMSource(document); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); return file; }/*from w ww. j av a 2 s. co m*/ }