save XML Content to a file - Java XML

Java examples for XML:DOM Element

Description

save XML Content to a file

Demo Code


//package com.java2s;

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

public class Main {
    public static void saveXMLContent(Document d, String path_to_file) {
        try {//from  ww  w.  j ava2  s . c  o  m
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer tf = tff.newTransformer();
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource ds = new DOMSource(d);
            StreamResult sr = new StreamResult(path_to_file);
            tf.transform(ds, sr);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Related Tutorials