Java tutorial
//package com.java2s; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; 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 { /** * @param dom * @param outFile */ public static void writeDomToFile(Document dom, File outFile) { try { TransformerFactory transFact = TransformerFactory.newInstance(); Transformer transformer = transFact.newTransformer(); DOMSource source = new DOMSource(dom); StreamResult result; result = new StreamResult( new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding"))); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException( "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e); } } }