Java tutorial
//package com.java2s; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { private static Transformer _transformer = null; public static void documentToFile(Document doc, File file, Charset charset) throws IOException { StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), charset)); DOMSource source = new DOMSource(doc); try { _transformer.transform(source, sr); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }