Java tutorial
//package com.java2s; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Logger; 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 { /** Class's logger. */ private static final Logger LOGGER = Logger.getLogger("sdp.common.XmlUtils"); /** * Output an XML document to the given file. * * @param doc XML document to write. * @param filename File to write to. */ public static void writeXmlDocument(Document doc, String filename) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); final FileOutputStream out = new FileOutputStream(new File(filename)); StreamResult result = new StreamResult(out); transformer.transform(source, result); out.close(); } catch (IOException e) { LOGGER.warning("Error closing stream or could open a file from writing an XML document."); e.printStackTrace(); } catch (TransformerConfigurationException e) { LOGGER.warning("TransformerConfigurationException thrown when writing XML document to a file."); e.printStackTrace(); } catch (TransformerException e) { LOGGER.warning("Transformer exception thrown when writing XML document to a file."); e.printStackTrace(); } } }