java - Jaxb child attributes with same name mapping to parent object -
java - Jaxb child attributes with same name mapping to parent object -
i have xml document in format:
<company> <employees> <employee firstname="xyz" lastname="abc"> <empid id="1"/> <department id="d1"/> <manager id="23"/> </employee> </employees> <departments> <department name="dept 1"> <deptid id="d1"/> </department> </departments> <company> i trying utilize jaxb unmarshal java unable determine way map employee id id field in employee class.
public class employee{ @xmlattribute private string firstname; @xmlattribute private string lastname; ??? @xmlid private string id; ??? @xmlidref private string managerid; }
any suggestions?
based on comment feedback, can effort marshal info using following:
create 2 classes represent id fields in object classes:
@xmlaccessortype(xmlaccesstype.field) @xmlrootelement(name="deptid") public class departmentid { @xmlattribute(name="id") // define attribute id @xmlid string id; @xmlvalue // define value, , set null string value = null; public departmentid() {} public void setid(string id) { this.id = id; } public string getid() { homecoming this.id; } } and
@xmlaccessortype(xmlaccesstype.field) @xmlrootelement(name="empid") public class employeeid { @xmlattribute(name="id") @xmlid string id; @xmlvalue string value = null; public employeeid() {} public void setid(string id) { this.id = id; } public string getid() { homecoming this.id; } } annotate employee class so: slight mods getter/setter methods may necessary, pretty crude, should work test below, as-is
@xmlaccessortype(xmlaccesstype.field) @xmlrootelement(name="employee") public class employee { @xmlelement(name="empid") private employeeid id; // employee id object used represent element @xmlattribute(name="firstname") private string firstname; @xmlattribute(name="lastname") private string lastname; @xmlelement(name="manager") private employeeid manager; // same manager @xmlelement(name="department") private departmentid department; // same dept public employee() { } public void setid(string id) { this.id = new employeeid(); this.id.setid(id); } public void setfirstname(string firstname) { this.firstname = firstname; } public void setlastname(string lastname) { this.lastname = lastname; } public void setmanager(employee manager) { this.manager = new employeeid(); this.manager.setid(manager.getid()); } public void setdepartment(department department) { this.department = new departmentid(); this.department.setid(department.getid()); } public string getid() { homecoming this.id.getid(); } public string getfirstname() { homecoming this.firstname; } public string getlastname() { homecoming this.lastname; } public employeeid getmanager() { homecoming this.manager; } public departmentid getdepartment() { homecoming this.department; } } a department class annotated this:
@xmlrootelement(name="department") @xmlaccessortype(xmlaccesstype.field) public class section { @xmlelement(name="deptid") private departmentid id; public department() {} public void setid(string id) { this.id = new departmentid(); this.id.setid(id); } public string getid() { homecoming this.id.getid(); } } a company class annotated this:
@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class company { @xmlelement(name="employee") private list<employee> employees; @xmlelement(name="department") private list<department> departments; public company() { employees = new arraylist<employee>(); departments = new arraylist<department>(); } public list<employee> getemployees() { homecoming this.employees; } public void setemployees(list<employee> employees) { this.employees = employees; } public list<department> getdepartments() { homecoming this.departments; } public void setdepartments(list<department> departments ) { this.departments = departments; } } with next test:
public class test { public static void main(string[] args) throws exception { company c = new company(); section d1 = new department(); d1.setid("d1"); section d2 = new department(); d2.setid("d2"); employee e1 = new employee(); e1.setid("1"); e1.setfirstname("joe"); e1.setlastname("smith"); e1.setdepartment(d1); employee e2 = new employee(); e2.setid("2"); e2.setfirstname("john"); e2.setlastname("doe"); e2.setmanager(e1); e2.setdepartment(d1); c.getemployees().add(e1); c.getemployees().add(e2); c.getdepartments().add(d1); c.getdepartments().add(d2); jaxbcontext context = jaxbcontext.newinstance(company.class); marshaller m = context.createmarshaller(); m.setproperty(marshaller.jaxb_formatted_output, true); m.marshal(c, system.out); } } the next xml produced:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <company> <employee firstname="joe" lastname="smith"> <empid id="1"/> <department id="d1"/> </employee> <employee firstname="john" lastname="doe"> <empid id="2"/> <manager id="1"/> <department id="d1"/> </employee> <department> <deptid id="d1"/> </department> <department> <deptid id="d2"/> </department> </company> since have embedded tag employees , departments want have couple classes handle level of nesting. see if works you.
see this reference , this reference additional info.
java xml jaxb
Comments
Post a Comment