Java tutorial
//package com.java2s; import org.w3c.dom.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.OutputStream; public class Main { public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { serializer = tfactory.newTransformer(); if (prettyPrint) { //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } if (!includeXmlDeclaration) { serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } DOMSource xmlSource = new DOMSource(node); StreamResult outputTarget = new StreamResult(out); serializer.transform(xmlSource, outputTarget); } catch (TransformerException e) { throw new RuntimeException(e); } } }