public final byte[] exportXMLAsByteArray(DocumentModel doc, CoreSession session) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DocumentWriter documentWriter = new XMLDocumentWriter(outputStream);
DocumentReader documentReader = new TypedSingleDocumentReader(session, doc);
DocumentPipe pipe = new DocumentPipeImpl();
pipe.setReader(documentReader);
pipe.setWriter(documentWriter);
try {
pipe.run();
} catch (IOException e) {
throw new NuxeoException("Error while trying to export the document to XML.", e);
} finally {
if (documentReader != null) {
documentReader.close();
}
if (documentWriter != null) {
documentWriter.close();
}
}
return outputStream.toByteArray();
}
}