Friday, December 28, 2007

Castor - Java Data Binding Framework

Castor is opens source data binding framework for Java. Castor provides Java to XML binding & Castor can map relational database into JDO( Java Data Object).
Using Castor,
You can convert the XML schema into Java Objects.
You can convert XML into Java Objects & Vice versa.
You can perform Java to RDBMS marshalling & unmarshalling.

Downloads
You need to download castor.jar,castor-commons.jar, castor-codegen.jar & other dependencies jar based on your requirements.
castor-codegen.jar is basically required for generating Java classes from XML Schema.

Java Object to XML by using Marshalling
First we will build a simple java class.
package com.myproject.commons;
import java.util.ArrayList;
import java.io.Serializable;

public class EmployeeData {
private String employeeID;
private String employeeName;
private String dept;
private String designation;
private ArrayList SME = null;

/**
* @return Returns the dept.
*/
public String getDept() {
return dept;
}
/**
* @param dept The dept to set.
*/
public void setDept(String dept) {
this.dept = dept;
}
/**
* @return Returns the designation.
*/
public String getDesignation() {
return designation;
}
/**
* @param designation The designation to set.
*/
public void setDesignation(String designation) {
this.designation = designation;
}
/**
* @return Returns the employeeID.
*/
public String getEmployeeID() {
return employeeID;
}
/**
* @param employeeID The employeeID to set.
*/
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
/**
* @return Returns the employeeName.
*/
public String getEmployeeName() {
return employeeName;
}
/**
* @param employeeName The employeeName to set.
*/
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public void addSME(String sme)
{
if(SME==null) {
SME = new ArrayList();
}
SME.add(sme);
}
/**
* @return Returns the list.
*/
public ArrayList getSME() {
return SME;
}
/**
* @param list The list to set.
*/
public void setSME(ArrayList list) {
this.SME = list;
}
}

Now you need a class to handle marshalling

package com.myproject.commons;
import java.io.FileWriter;
import org.exolab.castor.xml.Marshaller;
import com.myproject.commons.EmployeeData;

public class TestMarshaller {
public TestMarshaller() {
super();
}
public static void main(String[] args) {
try{
EmployeeData emp = new EmployeeData();
emp.setEmployeeID("E0123");
emp.setEmployeeName("xyz");
emp.setDept("Technology");
emp.setDesignation("Senior Analyst");
emp.addSME("J2EE");
emp.addSME("XML");
FileWriter file = new FileWriter("employee-data.xml");
Marshaller.marshal(emp,file);
System.out.println("XML Generated");
}
catch(Exception e) {
System.out.println("Exception occured:"+e.getMessage());
}}}

Locate the generated xml file ie employee-data.xml.It should look like this
<?xml version="1.0" encoding="UTF-8"?>
<employee-data>
<employee-iD>E0123</employee-iD>
<employee-name>xyz</employee-name>
<dept>Technology</dept>
<designation>Senior Analyst</designation>
<SME>J2EE</SME>
<SME>XML</SME>
</employee-data>

XML to Java Object using Unmarshalling
We will build a simple class to unmarshall the xml to Java object
package com.myproject.commons;
import java.io.FileReader;
import org.exolab.castor.xml.Unmarshaller;
public class TestUnmarshaller {

public TestUnmarshaller() {
super();
}
public static void main(String[] args) {
try{
FileReader reader = new FileReader("employee-data.xml");
EmployeeData emp = (EmployeeData)Unmarshaller.unmarshal(EmployeeData.class,reader);

System.out.println("Employee Id:"+emp.getEmployeeID());
System.out.println("Employee Name:"+emp.getEmployeeName());
}catch(Exception e){
System.out.println("Exception occurred:"+e.getMessage());
}}}

The Output looks like below
Employee Id:E0123
Employee Name:xyz

XML Schema to Java object
Castor's Source Code Generator creates a set of Java classes which represent an object model for an XML Schema.
Here is the simple command to generate the Java objects from XML schema.
C:\Castor>java org.exolab.castor.builder.SourceGeneratorMain -i schema name - package name

No comments: