Here you can find the source of documentToString(Document doc)
Parameter | Description |
---|---|
doc | the Document to convert |
private static String documentToString(Document doc) throws TransformerConfigurationException, TransformerException
//package com.java2s; /*//from w ww. j a v a 2 s. co m * This file is part of "Tweety", a collection of Java libraries for * logical aspects of artificial intelligence and knowledge representation. * * Tweety is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * 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. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import java.io.StringWriter; import org.w3c.dom.Document; public class Main { /** * Convert from {@code Document} to {@code String} * @param doc the {@code Document} to convert * @return the resulting {@code String} */ private static String documentToString(Document doc) throws TransformerConfigurationException, TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } }