Here you can find the source of writeDom(Document doc, Writer w)
public static void writeDom(Document doc, Writer w) throws Exception
//package com.java2s; /*//from w w w. j ava 2 s . co m * Copyright 2013 Keith D Swenson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris, * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava, * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi */ import java.io.OutputStream; import java.io.Writer; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { private static TransformerFactory transformerFactory = null; private static final Long mutex2 = new Long(2); public static void writeDom(Document doc, OutputStream out) throws Exception { DOMSource docSource = new DOMSource(doc); Transformer transformer = getXmlTransformer(); transformer.transform(docSource, new StreamResult(out)); } public static void writeDom(Document doc, Writer w) throws Exception { DOMSource docSource = new DOMSource(doc); Transformer transformer = getXmlTransformer(); transformer.transform(docSource, new StreamResult(w)); } private static Transformer getXmlTransformer() throws Exception { /* * CDATA_SECTION_ELEMENTS | cdata-section-elements = expanded names. * DOCTYPE_PUBLIC | doctype-public = string. DOCTYPE_SYSTEM | * doctype-system = string. ENCODING | encoding = string. INDENT | * indent = "yes" | "no". MEDIA_TYPE | media-type = string. METHOD | * method = "xml" | "html" | "text" | expanded name. * OMIT_XML_DECLARATION | omit-xml-declaration = "yes" | "no". * STANDALONE | standalone = "yes" | "no". VERSION | version = nmtoken. */ initTransformer(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); try { transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (IllegalArgumentException e) { // If the property is not supported, and is not qualified with a // namespace then // it throws IllegalArgumentException. we do not have to re-throw // this exception. } return transformer; } private static final void initTransformer() throws TransformerFactoryConfigurationError, TransformerConfigurationException { if (transformerFactory == null) { synchronized (mutex2) { transformerFactory = TransformerFactory.newInstance(); } } } }