mclas = TestSimpleStAX.class.getClassLoader().loadClass(mname);
} catch (ClassNotFoundException ex) {
System.err.println("Class " + mname + " not found");
return false;
}
IBindingFactory bfact = BindingDirectory.getFactory(mclas);
// unmarshal document to construct objects
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object obj = uctx.unmarshalDocument(new FileInputStream(fin), null);
if (!mclas.isInstance(obj)) {
System.err.println("Unmarshalled result not expected type");
return false;
}
// determine encoding of input document
String enc = ((UnmarshallingContext)uctx).getInputEncoding();
if (enc == null) {
enc = "UTF-8";
}
// marshal root object back out to document in memory
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);
}