Java tutorial
//package com.java2s; /** * Copyright 2015 Yahoo Inc.<br> * Licensed under the terms of the MIT license. Please see LICENSE file at the root of this project for terms. * <p/> * * @author yuvalp@yahoo-inc.com * */ import java.io.IOException; import java.io.StringWriter; import javax.xml.transform.OutputKeys; 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 { public static String writeDocumentToString(Document document) { try { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); try { StreamResult streamResult = new StreamResult(writer); DOMSource source = new DOMSource(document); trans.transform(source, streamResult); } catch (TransformerException e) { e.printStackTrace(); return null; } finally { writer.close(); } return writer.toString(); } catch (TransformerConfigurationException | IOException e) { e.printStackTrace(); return null; } } }