Somewhat curiously, it is not so easy to write a DOM tree to an output stream. The easiest approach is to use the Extensible Stylesheet Language Transformations (XSLT) API. For more information about XSLT, turn to Section 2.8, “XSL Transformations,” on p. 173. Right now, consider the code that follows a magic incantation to produce XML output.
We apply the do-nothing transformation to the document and capture its output. To include a
DOCTYPE node in the output, we also need to set the
PUBLIC identifiers as output properties.
// construct the do-nothing transformation Transformer t = TransformerFactory.newInstance().newTransformer(); // set output properties to get a DOCTYPE node ...