Java tutorial
//package com.java2s; /* (c) 2015 Open Source Geospatial Foundation - all rights reserved * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; 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.Node; public class Main { /** * Print a DOM tree to an output stream or if there is an exception while doing so, print the * stack trace. * * @param dom * @param os */ public static void printDom(Node dom, OutputStream os) { Transformer trans; PrintWriter w = new PrintWriter(os); try { TransformerFactory fact = TransformerFactory.newInstance(); trans = fact.newTransformer(); trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os))); } catch (TransformerException e) { w.println("An error ocurred while transforming the given DOM:"); e.printStackTrace(w); } } }