Here you can find the source of write(Document doc)
public static String write(Document doc) throws Exception
//package com.java2s; /*//from w w w . j a v a 2 s . c om * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; public class Main { public static String write(Document doc) throws Exception { return write(doc.getDocumentElement()); } public static String write(Element e) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); write(e, baos); return new String(baos.toByteArray(), "UTF-8"); } public static void write(Element root, OutputStream os) throws Exception { Writer writer = new OutputStreamWriter(os); write(root, writer); } /** * @param root * @param writer * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Element root, Writer writer) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { Source input = new DOMSource(root); write(input, writer); } /** * @param reader * @param parser * @param output * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Reader reader, XMLReader parser, Result output) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { SAXSource input = new SAXSource(); InputSource inputSource = new InputSource(); inputSource.setCharacterStream(reader); input.setInputSource(inputSource); input.setXMLReader(parser); write(input, output); } /** * @param parser * @param writer * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Reader reader, XMLReader parser, Writer writer) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { Result output = new StreamResult(writer); write(reader, parser, output); } public static void write(Source input, Result output) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { write(input, output, true); } public static void write(Source input, Result output, boolean indent) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { boolean omitxmldeclaration = true; Transformer idTransform = TransformerFactory.newInstance().newTransformer(); if (omitxmldeclaration) { idTransform.setOutputProperty("omit-xml-declaration", "yes"); } idTransform.setOutputProperty("encoding", "UTF-8"); if (indent) { idTransform.setOutputProperty("indent", "yes"); try { idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (Exception e) { // } } idTransform.transform(input, output); } /** * @param input * @param os * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Source input, Writer writer) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { Result output = new StreamResult(writer); write(input, output); } }