Here you can find the source of saveDocumentToFile(Document doc, File file)
public static void saveDocumentToFile(Document doc, File file) throws Exception
//package com.java2s; import java.io.File; import java.io.FileOutputStream; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; 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 void saveDocumentToFile(Document doc, File file) throws Exception { if (file.getParent() != null) { mkdirs(file.getParent());// ww w. j a v a 2 s .c om } FileOutputStream fos = new FileOutputStream(file); Source source = new DOMSource(doc); Result result = new StreamResult(fos); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); xformer.transform(source, result); fos.close(); } public static void mkdirs(String pathName) { File file = new File(pathName); file.mkdirs(); } }