Java tutorial
//package com.java2s; import org.w3c.dom.Document; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import javax.xml.transform.Result; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; public class Main { public static void DocumentToFile(final Document doc, File file) { // FileWriter writer = null; OutputStreamWriter outputStreamWriter = null; try { // writer = new FileWriter(file); FileOutputStream fileOutputStream = new FileOutputStream(file); outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); } catch (Exception e) { System.err.println(e); return; } //Result l_s = new StreamResult(writer); Result l_s = new StreamResult(outputStreamWriter); doc.normalize(); try { TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s); outputStreamWriter.close(); //writer.close(); } catch (Exception e) { System.err.println(e); return; } } }