Castor (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.

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 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

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

Sample mapping file may look like as follows:


Version 1.0//EN" "http://Castor.exolab.org/mapping.dtd">



type="javajaxb.generated.hr.Employee">




type="integer">


type="java.lang.String">


type="javajaxb.generated.hr.Address">


type="javajaxb.generated.hr.Organization">


type="javajaxb.generated.hr.Office">




type="java.lang.String">


type="java.lang.String">


type="java.lang.String">


type="java.lang.String">


type="java.lang.String">




type="integer">


type="javajaxb.generated.hr.Address">




type="integer">


type="java.lang.String">