Java tutorial
//package com.java2s; import java.io.File; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.xml.transform.OutputKeys; 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 { private static void SaveCustomerFile(Document CustomerDoc, JFrame mainFrame) { try { TransformerFactory Factory = TransformerFactory.newInstance(); Transformer Trans = Factory.newTransformer(); DOMSource source = new DOMSource(CustomerDoc); File f = new File("."); String FilePath = f.getAbsoluteFile().getParent() + "\\Customers.xml"; f = new File(FilePath); if (f.exists()) { f.delete(); } StreamResult Result = new StreamResult(f); Trans.setOutputProperty(OutputKeys.INDENT, "yes"); Trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "7"); Trans.transform(source, Result); } catch (TransformerException ex) { System.out.println(ex.getMessage()); JOptionPane.showMessageDialog(mainFrame, "There Was an Error Saving The File. Please Restart the Application."); System.exit(1); } } }