XML Data to Java Bean Conversion in Java

XStream :

Stream is a simple library to serialize objects to XML and back again. 
Download XStream Jar :   http://xstream.codehaus.org/download.html 

Syntax :

For Object Creation:

XStream xStream = new XStream();

XML Conversion:

employee = (Object) xStream.fromXML(XMLString);

Clear Example:

Consider Bean Employee:

private static class Employee {
private String ename;
private String eno;
private String eaddress;

public Employee() {
}

public Employee(String ename, String eno, String eaddress) {
this.ename = ename;
this.eno = eno;
this.eaddress = eaddress;
}

public String getEaddress() {
return eaddress;
}

public void setEaddress(String eaddress) {
this.eaddress = eaddress;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public String getEno() {
return eno;
}

public void setEno(String eno) {
this.eno = eno;
}

Conversion Main Class Example

public static void main(String[] args) {
      String XMLData = "<Employee><ename>God</ename><eno>01</eno><eaddress>Earth</eaddress></Employee>";
      XStream xStream = new XStream();
      xStream.alias("Employee", Employee.class);
      Employee employee = new Employee();
      System.out.println("XMLData : "+XMLData);
      employee = (Employee) xStream.fromXML(XMLData);
      System.out.println("Employee Name :"+employee.getEname());
      System.out.println("Employee No :"+employee.getEno());
      System.out.println("Employee Address :"+employee.getEaddress());
   }

Sample Output:

XMLData : <Employee><ename>God</ename><eno>01</eno><eaddress>Earth</eaddress></Employee>
Employee Name :God
Employee No :01
Employee Address :Earth

Note : Here, problem is XML Tag name and Java Variable Must be same, if different means, it didn't convert. 

Alias For Class Name:

Add these line, you can get XML without Package Name
xStream.alias("Employee", Employee.class);

Consider Different Type of XML with Different Tag-names:

<Employee>
<employeename>God</employeename>
<employeeno>01</employeeno>
<employeeaddress>Earth</employeeaddress>
</Employee>

Alias Name For Class Field/Variable

Syntax:

xStream.aliasField("aliasname", classname, "fieldname");

Example

xStream.aliasField("employeename", Employee.class, "ename");

Main Class For Alias Class Name/Root Name and Alias Field Name:
public static void main(String[] args) {
      String XMLData = "<Employee><employeename>God</employeename><employeeno>01</employeeno><employeeaddress>Earth</employeeaddress></Employee>";
      XStream xStream = new XStream();
      xStream.alias("Employee", Employee.class);
      xStream.aliasField("employeename", Employee.class, "ename");
      xStream.aliasField("employeeno", Employee.class, "eno");
      xStream.aliasField("employeeaddress", Employee.class, "eaddress");
      Employee employee = new Employee();
      System.out.println("XMLData : "+XMLData);
      employee = (Employee) xStream.fromXML(XMLData);
      System.out.println("Employee Name :"+employee.getEname());
      System.out.println("Employee No :"+employee.getEno());
      System.out.println("Employee Address :"+employee.getEaddress());
   }

Sample Output:

XMLData : <Employee><employeename>God</employeename><employeeno>01</employeeno><employeeaddress>Earth</employeeaddress></Employee>
Employee Name :God
Employee No :01
Employee Address :Earth

Note: these way you can convert all type of xml to Java bean.
More Information :  http://xstream.codehaus.org/