try {
if (context == null) {
context = JAXBContext.newInstance(elValue.getClass());
}
Object mObj = elValue;
Marshaller u = context.createMarshaller();
u.setProperty(Marshaller.JAXB_ENCODING , "UTF-8");
u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (elValue.getClass().isAnnotationPresent(XmlRootElement.class)) {
String packageName = elValue.getClass().getPackage().getName();
Class<?> objectFactory = Class.forName(packageName + ".ObjectFactory", false,
elValue.getClass().getClassLoader());
Method methods[] = objectFactory.getDeclaredMethods();
for (Method method : methods) {
if (method.getParameterTypes().length == 1
&& method.getParameterTypes()[0].equals(elValue.getClass())) {
XmlElementDecl elementDecl = method.getAnnotation(XmlElementDecl.class);
if (null != elementDecl) {
QName elementType = new QName(elementDecl.namespace(), elementDecl.name());
if (elementType.equals(elNname)) {
mObj = method.invoke(objectFactory.newInstance(),
elValue);
}
}
}
}
} else {
mObj = JAXBElement.class.getConstructor(new Class[] {QName.class, Class.class, Object.class})
.newInstance(elNname, mObj.getClass(), mObj);
}
u.setSchema(schema);
u.marshal(mObj, destNode);
} catch (MarshalException me) {
// It's helpful to include the cause in the case of
// schema validation exceptions.
String message = "Marshalling error ";
if (me.getCause() != null) {