DocumentSource documentSource = new DocumentSource(document);
transformer.transform(documentSource, streamResult);
// now lets parse it back again via JAXP
DocumentResult documentResult = new DocumentResult();
StreamSource streamSource = new StreamSource(new StringReader(buffer
.toString()));
transformer.transform(streamSource, documentResult);
Document newDocument = documentResult.getDocument();
// lets ensure names are same
newDocument.setName(document.getName());
assertDocumentsEqual(document, newDocument);