Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; 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 void saveXml(Document modDoc, String path) throws TransformerException, IOException { Writer writer = null; try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(modDoc); writer = getFileWriter(path); Result dest = new StreamResult(writer); aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); aTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); aTransformer.transform(src, dest); } catch (TransformerException e) { throw e; } finally { writer.close(); } } public static FileWriter getFileWriter(String filePath) { File xmlFile = new File(filePath); FileWriter writer = null; try { writer = new FileWriter(xmlFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return writer; } }