I've never really used reflection before and am attempting something I'm not sure is possible. Basically, I'm trying to write a method that takes an Object as a parameter, and then attempts to marshal that object regardless of its type. I can't figure out how to get the type to use when instantiating the generic JAXBElement<T> object. Is this possible? My attempt:
String marshalObject(Object obj) { Class c = obj.getClass(); Type t = (Type) c; QName _QNAME = new QName("http://www.acme.com/ImportExport", c.getName()); StringWriter sw = new StringWriter(); try { ObjectFactory of = new ObjectFactory(); JAXBElement<?> jaxElement = new JAXBElement<t>(_QNAME, c, null, obj); JAXBContext context = JAXBContext.newInstance( c ); Marshaller m = context.createMarshaller(); m.marshal( jaxElement, sw ); } catch( JAXBException jbe ){ System.out.println("Error marshalling object: " + jbe.toString()); return null; } return sw.toString(); }