Here you can find the source of xmlDocToString(Document doc)
Parameter | Description |
---|---|
doc | XML Document object. |
public static String xmlDocToString(Document doc)
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 ww w. java2s . com*/ * Place the contents of an XML Document into a string object. * * @param doc * XML Document object. * @return String contents of the document. */ public static String xmlDocToString(Document doc) { String output = null; TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = transformerFactory.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); output = writer.getBuffer().toString().replaceAll("\n|\r", ""); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output; } }