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

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

Description

Saves a DOM document to an XML file in utf-8.

License

Open Source License

Parameter

Parameter Description
document DOM document.
path Output file path.

Exception

Parameter Description
TransformerException , IOException

Declaration


public static void save(Document document, String path) throws TransformerException, IOException 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.io.*;

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

public class Main {
    /**   Saves a DOM document to an XML file in utf-8.
     */*from   w w w  . j a va 2s  .c  o m*/
     *   @param   document   DOM document.
     *   @param   path      Output file path.
     *
     *   @throws   TransformerException, IOException
     */

    public static void save(Document document, String path) throws TransformerException, IOException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        PrintWriter pw = new PrintWriter(new FileOutputStream(path));
        StreamResult destination = new StreamResult(pw);
        transformer.transform(source, destination);
        pw.close();
    }

    /**   Saves a DOM document to an XML file in utf-8.
     *
     *   @param   document   DOM document.
     *   @param   dtdName      The DTD name.
     *   @param   path      Output file path.
     *
     *   @throws   TransformerException, IOException
     */

    public static void save(Document document, String dtdName, String path)
            throws TransformerException, IOException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdName);
        PrintWriter pw = new PrintWriter(new FileOutputStream(path));
        StreamResult destination = new StreamResult(pw);
        transformer.transform(source, destination);
        pw.close();
    }
}

Related

  1. save(Document doc, File file)
  2. save(Document doc, String file, String encoding)
  3. save(Document document, OutputStream out, Properties outputProperties)
  4. save(Document document, OutputStream outputStream)
  5. save(Document document, String fileName)
  6. save(final String path, final Document xml)
  7. save(String filename, Document document)
  8. saveDoc(Document doc, OutputStream output)
  9. saveDoc(Document doc, String fileName)