Java tutorial
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); you may 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.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { public static String DocToString(Document doc) throws TransformerFactoryConfigurationError, TransformerException { if (doc == null) return new String(""); String xmlString = ""; Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); xmlString = result.getWriter().toString(); return xmlString; } }