java - Exception converting object to XML using jaxb -
i trying build xml object using jaxb.
but missing because exception:
javax.xml.bind.marshalexception - linked exception: [com.sun.istack.internal.saxexception2: class employee nor of super class known context. javax.xml.bind.jaxbexception: class employee nor of super class known context.]
@xmlrootelement(name = "employee") public class employee { private string name; private string employeeid; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getemployeeid() { return employeeid; } public void setemployeeid(string employeeid) { this.employeeid = employeeid; } } @xmlrootelement(name = "data") public class data { public data() { } private list employees; @xmlelementwrapper(name = "employeelist") @xmlelement(name = "employee") public list getemployees() { return employees; } public void setemployees(list employees) { this.employees = employees; } } public static void main(string[] args) { arraylist list = new arraylist(); employee e1 = new employee(); e1.setname("name"); e1.setemployeeid("1"); list.add(e1); data data = new data(); data.setemployees(list); jaxbcontext context; bytearrayoutputstream outstream = new bytearrayoutputstream(); try { context = jaxbcontext.newinstance(data.class); marshaller m = context.createmarshaller(); m.setproperty(marshaller.jaxb_formatted_output, boolean.true); m.marshal(data, outstream); } catch (jaxbexception e) { e.printstacktrace(); } }
you need 1 of following jaxb (jsr-222) implementation aware employees
property on data
class contains instances of employee
.
@xmlelementwrapper(name = "employeelist") @xmlelement(name = "employee", type=employee.class) public list getemployees() { return employees; }
or
@xmlelementwrapper(name = "employeelist") @xmlelement(name = "employee") public list<employee> getemployees() { return employees; }
Comments
Post a Comment