Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; public class Main { public static File saveToFile(String filename, Document document) throws TransformerException { // Prepare the DOM document for writing Source source = new DOMSource(document); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); return file; } public static File saveToFile(File file, Document document) throws TransformerException { // Prepare the DOM document for writing Source source = new DOMSource(document); // Prepare the output file Result result = new StreamResult(file.toURI().getPath()); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); return file; } }