Here you can find the source of writeXmlToString(Document xmlDoc)
Parameter | Description |
---|---|
xmlDoc | - the document to write. |
Parameter | Description |
---|---|
TransformerException | an exception |
public static String writeXmlToString(Document xmlDoc) throws TransformerException
//package com.java2s; //License from project: Apache License import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { /**/*from w w w .j a v a 2s . c o m*/ * writeXmlToString - write the given XML document to a string. * * @param xmlDoc - the document to write. * @return - the string representation of the XML document. * @throws TransformerException */ public static String writeXmlToString(Document xmlDoc) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDoc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; } }