Source source = new DocumentSource(document);
// use pretty print format and a buffer for the result
OutputFormat format = OutputFormat.createCompactFormat();
StringWriter buffer = new StringWriter();
Result result = new XMLResult(buffer, format);
// now lets transform
transformer.transform(source, result);
String text = buffer.toString();