Java XML Document Save to File saveXMLDocument(Document document, String path)

Here you can find the source of saveXMLDocument(Document document, String path)

Description

save XML Document

License

Open Source License

Declaration

public static void saveXMLDocument(Document document, String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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 {
    public static void saveXMLDocument(Document document, String path) {
        saveXMLDocument(document, new File(path));
    }//from   w w  w . j  av  a 2  s .  c  o m

    public static void saveXMLDocument(Document document, File file) {
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(file);
            Source source = new DOMSource(document);
            Result result = new StreamResult(output);
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(source, result);
        } catch (Exception ex) {
            try {
                output.close();
            } catch (IOException io) {
            }
        } finally {
            try {
                output.close();
            } catch (IOException io) {
            }
        }
    }
}

Related

  1. saveXML(Document doc, File outfile, boolean indent)
  2. saveXml(Document doc, String filename)
  3. saveXML(Document document, String filename)
  4. saveXml(Document dom, String filePath)
  5. saveXml(Document modDoc, String path)
  6. saveXMLDocument(String fileName, Document doc)
  7. saveXmlToStreamWriter(Document doc)