Java - Write a XML DOM Document (org.w3c.dom.Document) in to a file

Very useful when you need to backup XML data at client side.
// This method writes a DOM document to a file
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // 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.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}

Popular posts from this blog

Java XML getElementById returning null, fix using XPath

Sending an Email to Multiple Recipients - Cc: and Bcc:

Store your 450GB data on an A4 size paper: THE RAINBOW TECHNOLOGY