// load a default transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
// use dom4j document as the source
Source source = new DocumentSource(document);
// use pretty print format and a buffer for the result
OutputFormat format = OutputFormat.createPrettyPrint();
StringWriter buffer = new StringWriter();
Result result = new XMLResult(buffer, format);