Need help in mapping XML to a Java Bean -


i have xml follows ...

<employeeid>323</employeeid> <name>samuel dcosta</name> <department>     <departmentid>2</departmentid>     <name>accounts</name> </department> <salary>11290</salary> 

i want map these values java beans have .... keys in xml match name of members in beans ..... tell me if there simple way in java please .... tools or components welcome ...

department ....

import java.io.serializable;  public class department implements serializable { private long departmentid;  private string name;  @override public string tostring() {     return "department [departmentid=" + departmentid + ", name=" + name + "]"; }  public long getdepartmentid() {     return departmentid; }  public void setdepartmentid(long departmentid) {     this.departmentid = departmentid; }  public string getname() {     return name; }  public void setname(string name) {     this.name = name; } } 

employee .....

import java.io.serializable;  public class employee implements serializable { private long employeeid;  private string name;  private department department;  private integer salary;  @override public string tostring() {     return "employee [employeeid=" + employeeid + ", name=" + name + ", department=" + department + ", salary="             + salary + "]"; }  public long getemployeeid() {     return employeeid; }  public void setemployeeid(long employeeid) {     this.employeeid = employeeid; }  public string getname() {     return name; }  public void setname(string name) {     this.name = name; }  public department getdepartment() {     return department; }  public void setdepartment(department department) {     this.department = department; }  public integer getsalary() {     return salary; }  public void setsalary(integer salary) {     this.salary = salary; } } 

i used original xml , 2 java beans- employee , department in test code

import java.util.arraylist; import java.util.list;  import cjm.component.cb.object.toobject;  public class employeeconversion { public static void main(string[] args) {     try     {         string xml = "<employeeid>323</employeeid><name>samuel dcosta</name><department><departmentid>2</departmentid><name>accounts</name></department><salary>11290</salary>";          list<object> objectlist = new arraylist<object>();          objectlist.add(new employee());         objectlist.add(new department());         // adding nested objects within employee bean list          employee employee = (employee) new toobject().converttoobject(xml, new employee(), objectlist); // carry out mapping bean          system.out.println(employee);     }     catch (exception e)     {         e.printstacktrace();     } } } 

output

-------- xml detected --------  -------- xml detected --------  -------- map created --------  -------- object created --------  employee [employeeid=323, name=samuel dcosta, department=department [departmentid=2, name=accounts], salary=11290] 

you need conversion box (go v1.1) http://capsulesforthejavamind.blogspot.in/2015/01/conversion-box.html

ping me results!!! :)


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -