Save Dom Document to a file path - Java XML

Java examples for XML:DOM

Description

Save Dom Document to a file path

Demo Code

// This program is free software; you can redistribute it and/or modify it
//package com.java2s;
import org.w3c.dom.*;
import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class Main {
    public static void SaveDom(Document dom, String filepath) {
        try {/*from   w ww . jav a2 s.  c  om*/
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();

            DOMSource domSource = new DOMSource(dom);

            StreamResult streamResult = new StreamResult(
                    new FileOutputStream(filepath));
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            transformer.transform(domSource, streamResult);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials