Java tutorial
//package com.java2s; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { /** * Writes a DOM document to a file. * * @param document * DOM document. * @param fileName * target file path. */ public static void writeDOMToFile(Document document, String fileName) { FileOutputStream fos; Source source; Result result; Transformer xformer; try { fos = new FileOutputStream(fileName); source = new DOMSource(document); result = new StreamResult(fos); xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } }