~/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 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
Giving an Agent an Inbox Without Giving It Your Mailbox
A dedicated email address limits which messages reach an agent; the harder details are untrusted content, sending authority and missing attachments.
August 20, 2026 | 6 min read
Your Webhook Returned 200. Did the Work Happen?
Tracing a WHOOP event through receipt, diagnostic capture, Sheets forwarding and reconciliation reveals what a successful response leaves unproved.
August 06, 2026 | 4 min read