IMarshallingContext mctx = bfact.createMarshallingContext();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
XMLOutputFactory ofact = XMLOutputFactory.newInstance();
XMLStreamWriter wrtr = ofact.createXMLStreamWriter(bos, enc);
mctx.setXmlWriter(new StAXWriter(bfact.getNamespaces(), wrtr));
mctx.marshalDocument(obj);
} catch (XMLStreamException e) {
throw new JiBXException("Error creating writer", e);
}