Here you can find the source of writeDocumentTo(Document doc, File file)
Parameter | Description |
---|---|
doc | The Document to write. |
file | The File to write to. |
public static void writeDocumentTo(Document doc, File file) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.*; public class Main { private static final TransformerFactory m_xformFactory = TransformerFactory.newInstance(); /**//w w w .j av a 2 s. c om * Writes the given {@link Document} to a {@link File}. * * @param doc The {@link Document} to write. * @param file The {@link File} to write to. * @see #encodeDocument(Document,boolean) * @see #writeDocumentTo(Document,OutputStream) */ public static void writeDocumentTo(Document doc, File file) throws IOException { final FileOutputStream fos = new FileOutputStream(file); try { writeDocumentTo(doc, fos); } finally { fos.close(); } } /** * Writes the given {@link Document} to an {@link OutputStream} using * UTF-8 encoding. * * @param doc The {@link Document} to write. * @param out The {@link OutputStream} to write to. * @see #encodeDocument(Document,boolean) * @see #writeDocumentTo(Document,File) */ public static void writeDocumentTo(Document doc, OutputStream out) throws IOException { final DOMSource source = new DOMSource(doc); final Writer writer = new OutputStreamWriter(out, "UTF-8"); final StreamResult result = new StreamResult(writer); final Transformer xform = createTransformer(); try { xform.transform(source, result); } catch (TransformerException e) { throw new IOException("Couldn't write XML: " + e.getMessage()); } } /** * Create a {@link Transformer} just the way we like it. * * @return Returns a new {@link Transformer}. */ private static Transformer createTransformer() { final Transformer xform; try { xform = m_xformFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IllegalStateException(e); } xform.setOutputProperty(OutputKeys.INDENT, "yes"); xform.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); return xform; } }