~/blog $ cat ./post.mdcat java-document-to-string.md
Java Document to String
|mcutler|1 min read
This is a very simple utility function to produce an indented XML document as a String given a Document. It throws a TransformerException if anything drastic goes wrong.
java
import java.io.*; import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; ... public static String documentToString( Document document ) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); transformer.transform(source, result); return sw.toString(); }
$ subscribe --newsletter
Practical AI engineering, in your inbox
Field notes for technical leaders building agents, evaluation systems, governance, and production infrastructure.
Related
What Belongs in Agent Memory
A durable handoff separates observations, hypotheses and unfinished work, then records what would make each observation stale.
October 09, 2026 | 4 min read
What Happened After the $8 Website Rebuild
The $7.99 rebuild bought a first version; the repository now shows the less photogenic work of preserving an archive and keeping its promises.
October 02, 2026 | 6 min read
What I Need Before I Approve an Agent’s Work
A review bundle should connect the requirement to evidence for the exact candidate and make the remaining decision explicit.
September 17, 2026 | 4 min read