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

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

Description

save

License

Open Source License

Declaration

public static void save(final String path, final Document xml) throws Exception 

Method Source Code

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

import java.io.FileOutputStream;

import javax.xml.transform.OutputKeys;
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 save(final String path, final Document xml) throws Exception {

        try {//from  w  w w .  ja  v  a 2s .  c o  m
            final Transformer tr = TransformerFactory.newInstance().newTransformer();
            tr.setOutputProperty(OutputKeys.INDENT, "yes");
            tr.setOutputProperty(OutputKeys.METHOD, "xml");
            tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            // tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "roles.dtd");
            tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            // send DOM to file
            tr.transform(new DOMSource(xml), new StreamResult(new FileOutputStream(path)));

        } catch (final Exception e) {
            e.printStackTrace();
            throw new Exception("XMLUtil - save: error");
        }
    }
}

Related

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