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.[1][2][3] Castor is one of the oldest data binding projects.[3]
Process flow
Basic process flows include class generation, marshalling, unmarshalling, etc.[2] Marshalling framework includes a set of ClassDescriptors and FieldDescription to describe objects.[3]
Class generation
Class generation is similar to JAXB and Zeus. Castor supports XML Schema instead of DTDs (DTDs are not supported by Castor).[2][3][4]
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.[2]
Additional features
Castor offers some additional features which are not present in JAXB. Additional features include:
Database and directory server mappings - mapping between databases and directory servers to Java
packagejavajaxb;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;// Castorimportorg.exolab.castor.xml.MarshalException;importorg.exolab.castor.xml.ValidationException;// Generated hr.xml classesimportjavajaxb.generated.hr.*;publicclassEmployeeLister{/** The descriptor to read in */privateFiledescriptor;/** The output file to write to */150privateFileoutputFile;/** The object tree read in */privateEmployeesemployees;publicEmployeeLister(Filedescriptor,FileoutputFile){employees=null;this.descriptor=descriptor;this.outputFile=outputFile;}publicvoidlist(booleanvalidate)throwsIOException,MarshalException,ValidationException{// Unmarshallemployees=Employees.unmarshal(newFileReader(descriptor));// Do some basic printingSystem.out.println("--- Employee Listing ---\n");Employee[]employeeList=employees.getEmployee();for(inti=0;i<employeeList.length;i++){Employeeemployee=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");}}publicstaticvoidmain(String[]args){try{if(args.length!=2){System.out.println("Usage: java javajaxb.EmployeeLister"+"[web.xml filename] [output.xml filename]");return;}EmployeeListerlister=newEmployeeLister(newFile(args[0]),newFile(args[1]));lister.list(true);}catch(Exceptione){e.printStackTrace();}}}