Here you can find the source of saveToXmlStr(Document doc)
Document
into XML String
Parameter | Description |
---|---|
doc | a parameter |
Parameter | Description |
---|---|
TransformerException | an exception |
public static String saveToXmlStr(Document doc) throws DOMException, TransformerException
//package com.java2s; import java.io.StringWriter; import javax.xml.transform.OutputKeys; 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.DOMException; import org.w3c.dom.Document; public class Main { /**//from w w w . j a v a2s .c o m * serializing a XML DOM <code>Document</code> into XML String * * @param doc * @return the string in XML format * @throws TransformerException */ public static String saveToXmlStr(Document doc) throws DOMException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = sw.toString(); return xmlString; } }