Java data binding framework
Castor is a data binding framework for Java with some features like Java to Java-to-XML binding, Java-to-SQL persistence, paths between Java objects, XML documents, relational tables, etc. Castor is one of the oldest data binding projects.
Process flow.
Basic process flows include class generation, marshalling, unmarshalling, etc. Marshalling framework includes a set of ClassDescriptors and FieldDescription to describe objects.
Class generation.
Class generation is similar to JAXB and Zeus. Castor supports XML Schema instead of DTDs (DTDs are not supported by Castor).
Unmarshalling and marshalling.
Unmarshalling and marshalling are dealt with marshall() and unmarshall() methods respectively. During marshalling, conversion process from Java to XML is carried out, and, during unmarshalling, conversion process from XML to Java is carried out. Mapping files are the equivalent of a binding schema, which allows to transforms names from XML to Java and vice versa.
Additional features.
Castor offers some additional features which are not present in JAXB. Additional features include:
Code samples.
Code for marshalling may look like as follows:
package javajaxb;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
// Castor
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
// Generated hr.xml classes
import javajaxb.generated.hr.*;
public class EmployeeLister {
 // Existing methods
 public void modify()
 throws IOException, MarshalException, ValidationException {
 // Add a new employee
 Employee employee = new Employee();
 employee.setName("Ben Rochester");
 Address address = new Address();
 address.setStreet1("708 Teakwood Drive");
 address.setCity("Flower Mound");
 address.setState("TX");
 address.setZipCode("75028");
 employee.addAddress(address);
 Organization organization = new Organization();
 organization.setId(43);
 organization.setName("Technical Services");
 employee.setOrganization(organization);
 Office office = new Office();
 office.setId(241);
 Address officeAddress = new Address();
 officeAddress.setStreet1("1202 Business Square");
 officeAddress.setStreet2("Suite 302");
 officeAddress.setCity("Dallas");
 officeAddress.setState("TX");
 officeAddress.setZipCode("75218-8921");
 office.setAddress(officeAddress);
 employee.setOffice(office);
 // Add employee to list
 employees.addEmployee(employee);
 // marshal
 employees.marshal(new FileWriter(outputFile));
 public static void main(String[] args) {
 try {
 if (args.length != 2) {
 System.out.println("Usage: java javajaxb.EmployeeLister
 "[web.xml filename] [output.xml filename]");
 return;
 EmployeeLister lister = 
 new EmployeeLister(new File(args[0]), new
File(args[1]));
 lister.list(true);
 lister.modify();
 } catch (Exception e) {
 e.printStackTrace();
 } 
Code for unmarshalling may look like as follows:
package javajaxb;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
// Castor
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
// Generated hr.xml classes
import javajaxb.generated.hr.*;
public class EmployeeLister {
 /** The descriptor to read in */
 private File descriptor;
 /** The output file to write to */
150
 private File outputFile;
 /** The object tree read in */
 private Employees employees;
 public EmployeeLister(File descriptor, File outputFile) {
 employees = null;
 this.descriptor = descriptor;
 this.outputFile = outputFile;
 public void list(boolean validate) 
 throws IOException, MarshalException, ValidationException {
 // Unmarshall
 employees = Employees.unmarshal(new FileReader(descriptor));
 // Do some basic printing
 System.out.println("--- Employee Listing ---\n");
 Employee[] employeeList = employees.getEmployee();
 for (int i=0; i ;employeeList.length; i++) {
 Employee employee = employeeList[i];
 System.out.println("Employee: " + employee.getName());
 System.out.println("Organization: " + 
 employee.getOrganization().getName());
 System.out.println("Office: " + 
 employee.getOffice().getAddress().getCity() + ", " +
 employee.getOffice().getAddress().getState() + "\n");
 public static void main(String[] args) {
 try {
 if (args.length != 2) {
 System.out.println("Usage: java javajaxb.EmployeeLister
 "[web.xml filename] [output.xml filename]");
 return;
 EmployeeLister lister = 
 new EmployeeLister(new File(args[0]), new
File(args[1]));
 lister.list(true);
 } catch (Exception e) {
 e.printStackTrace();
 } 
Sample mapping file may look like as follows:
 ;?xml version="1.0"? ;
 ;!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "[URL] ;
 ;mapping ;
 ;class name="javajaxb.generated.hr.Employees" ;
 ;map-to xml="emp-list"/ ;
 ;field name="Employee"
 type="javajaxb.generated.hr.Employee" ;
 ;bind-xml name="emp" / ;
 ;/field ;
 ;/class ;
 ;class name="javajaxb.generated.hr.Employee" ;
 ;field name="Id"
 type="integer" ;
 ;bind-xml name="emp-id" node="attribute"/ ;
 ;/field ;
 ;field name="name"
 type="java.lang.String" ;
 ;bind-xml name="emp-name" node="attribute"/ ;
 ;/field ;
 ;field name="Address"
 type="javajaxb.generated.hr.Address" ;
 ;bind-xml name="emp-address" / ;
 ;/field ;
 ;field name="Organization"
 type="javajaxb.generated.hr.Organization" ;
 ;bind-xml name="emp-org"/ ;
 ;/field ;
 ;field name="Office"
 type="javajaxb.generated.hr.Office" ;
 ;bind-xml name="emp-office"/ ;
 ;/field ;
 ;/class ;
 ;class name="javajaxb.generated.hr.Address" ;
 ;field name="Street1"
 type="java.lang.String" ;
 ;bind-xml name="line-1" node="element"/ ;
 ;/field ;
 ;field name="Street2"
 type="java.lang.String" ;
 ;bind-xml name="line-2" node="element"/ ;
 ;/field ;
 ;field name="City"
 type="java.lang.String" ;
 ;bind-xml name="city" node="element"/ ;
 ;/field ;
 ;field name="State"
 type="java.lang.String" ;
 ;bind-xml name="state" node="element"/ ;
 ;/field ;
 ;field name="ZipCode"
 type="java.lang.String" ;
 ;bind-xml name="zip-code" node="element"/ ;
 ;/field ;
 ;/class ;
 ;class name="javajaxb.generated.hr.Office" ;
 ;field name="Id"
 type="integer" ;
 ;bind-xml name="office-id" node="attribute"/ ;
 ;/field ;
 ;field name="Address"
 type="javajaxb.generated.hr.Address" ;
 ;bind-xml name="office-address" node="element"/ ;
 ;/field ;
 ;/class ;
 ;class name="javajaxb.generated.hr.Organization" ;
 ;field name="Id"
 type="integer" ;
 ;bind-xml name="org-id" node="element"/ ;
 ;/field ;
 ;field name="Name"
 type="java.lang.String" ;
 ;bind-xml name="org-name" node="element"/ ;
 ;/field ;
 ;/class ;
 ;/mapping ; 
References.
 ;templatestyles src="Reflist/styles.css" / ;