Java XML Document Save to File saveXml(Document doc, String filename)

Here you can find the source of saveXml(Document doc, String filename)

Description

save Xml

License

Apache License

Declaration

public static void saveXml(Document doc, String filename) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintWriter;

public class Main {
    public static void saveXml(Document doc, String filename) {
        try {/*ww w .j a  v a 2s  .  c o  m*/
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(doc);
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
            StreamResult result = new StreamResult(pw);
            transformer.transform(source, result);
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. saveToFile(String filename, Document document)
  2. saveToXml(Document doc, OutputStream os)
  3. saveToXmlStr(Document doc)
  4. saveXML(Document doc, ByteArrayOutputStream outputStreamXML)
  5. saveXML(Document doc, File outfile, boolean indent)
  6. saveXML(Document document, String filename)
  7. saveXml(Document dom, String filePath)
  8. saveXml(Document modDoc, String path)
  9. saveXMLDocument(Document document, String path)