Java tutorial
//package com.java2s; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; 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 { public static boolean saveDocument(String fileName, Document doc) { System.out.println("Saving XML file... " + fileName); // open output stream where XML Document will be saved File xmlOutputFile = new File(fileName); FileOutputStream fos; Transformer transformer; try { fos = new FileOutputStream(xmlOutputFile); } catch (FileNotFoundException e) { System.out.println("Error occured: " + e.getMessage()); return false; } // Use a Transformer for output TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { System.out.println("Transformer configuration error: " + e.getMessage()); return false; } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(fos); // transform source into result will do save try { transformer.transform(source, result); } catch (TransformerException e) { System.out.println("Error transform: " + e.getMessage()); } return true; } }