Here you can find the source of save(Document document, String path)
Parameter | Description |
---|---|
document | DOM document. |
path | Output file path. |
Parameter | Description |
---|---|
TransformerException | , IOException |
public static void save(Document document, String path) throws TransformerException, IOException
//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(); } }