Java tutorial
//package com.java2s; import java.io.File; import java.util.Properties; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; 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 { private static boolean writeTo(Document doc, String fileName) throws Exception { boolean isOver = false; DOMSource doms = new DOMSource(doc); File f = new File(fileName); StreamResult sr = new StreamResult(f); try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(properties); t.transform(doms, sr); isOver = true; } catch (TransformerConfigurationException tce) { tce.printStackTrace(); } catch (TransformerException te) { te.printStackTrace(); } return isOver; } }