Here you can find the source of documentToString(Document document)
public static String documentToString(Document document) throws Exception
//package com.java2s; /**/*from w w w . jav a 2 s. c om*/ * Copyright (C) 2014 4th Line GmbH, Switzerland and others * * The contents of this file are subject to the terms of the * Common Development and Distribution License Version 1 or later * ("CDDL") (collectively, the "License"). You may not use this file * except in compliance with the License. See LICENSE.txt for more * information. * * This program 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. */ import org.w3c.dom.Document; 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 java.io.StringWriter; public class Main { public static String documentToString(Document document) throws Exception { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); document.setXmlStandalone(true); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); StringWriter out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); } }