java - Class Casting during Marshalling of JAXB -
objective: trying pass class through parameter c
class use marshaling directly.
error: [com.sun.istack.internal.saxexception2: unable marshal type "java.lang.class" element because missing @xmlrootelement annotation]
this error given @ line pointed arrow(-->)
comments: if try change c
@ line 8 & 9 expression actual class works fine. there way without doing this. class casting no option think.
public xmlmarshaller(class c){ try { jaxbcontext jaxbcontext = jaxbcontext.newinstance(c); marshaller marshaller = jaxbcontext.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); --> marshaller.marshal(c, new file("xmldyna/asd.xml")); --> marshaller.marshal(c, system.out); } catch (jaxbexception e) { e.printstacktrace(); } }
this other class in case thinks error missing @xmlrootelement annotations.
@xmlrootelement( name = "dynamic") public class dynamicloader { @xmlelement(name = "name") public string name = "asdasd"; public dynamicloader(){ xmlmarshaller marshaller = new xmlmarshaller(this.getclass()); } public string getname(){ return name; }
you should pass object
parameter instead of class
. make sure pass instance of class in instead of class itself. bootstrap jaxbcontext
on object.getclass()
.