Here you can find the source of printXMLDocument(Document document)
Parameter | Description |
---|---|
XML | Document |
public static String printXMLDocument(Document document)
//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 { /** /* www.j av a 2 s . c o m*/ * @param XML Document * @return string containing the given XML Document contents. */ public static String printXMLDocument(Document document) { String XMLString = new String(); StringWriter stringWriter = new StringWriter(); DOMSource source = new DOMSource(document); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = transformerFactory.newTransformer(); StreamResult result = new StreamResult(stringWriter); transformer.transform(source, result); StringBuffer stringBuffer = stringWriter.getBuffer(); XMLString = stringBuffer.toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return XMLString; } }