StringWriter sw = HtmlSerialization.createWriter(doc);
if (doc.getDoctype() != null) {
HtmlSerialization.outputDocType(doc.getDoctype(), sw);
}
RenderContext renderContext =
new RenderContext(new Concatenator(sw, null))
// More compact but needs charset set correctly.
.withAsciiOnly(false)
.withMarkupRenderMode(MarkupRenderMode.HTML);
// Use render unsafe in order to retain comments in the serialized HTML.