castor是一种将java对象和XML自动绑定的开源软件。它可以在java对象、XML文本、SQL数据表以及LDAP目录之间绑定。Castor几乎是JAXB的替代品。Castor是ExoLab Group下面的一个开放源代码的项目,它主要实现的是O/R映射功能。它主要API和数据接口为:JDO-like, SQL, OQL, JDBC, LDAP, XML, DSML。它支持分布式目录事务处理和时间;提供处理XML、Directory、XADirectory的类库,提供从XML到JAVA类的转换机制。Castor(http://castor.exolab..org/)是一种将Java对象和XML自动绑定的开源软件。它可以在Java对象、XML文本、SQL数据表以及LDAP目录之间绑定。
castor-1.0.1-xml.jar包下载地址:
http://www.castor.org/download.html
xercesImpl.jar包下载地址:
http://archive.apache.org/dist/xml/xerces-j/
本文提供了两种包的下载,可以下载测试看看
基本的bean:
public class FlightBean {
private String m_carrier; private int m_number; private String m_departure; private String m_arrival; public FlightBean() { } public void setCarrier(String carrier) { m_carrier = carrier; } public String getCarrier() { return m_carrier; } public void setNumber(int number) { m_number = number; } public int getNumber() { return m_number; } public void setDepartureTime(String time) { m_departure = time; } public String getDepartureTime() { return m_departure; } public void setArrivalTime(String time) { m_arrival = time; } public String getArrivalTime() { return m_arrival; } }
使用的mapping的匹配方式,XML的mapping方式:
<!DOCTYPE databases PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.exolab.org/mapping.dtd"> <mapping> <description>Basic mapping example</description> <class name="FlightBean" auto-complete="true"> <map-to xml="flight"/> <field name="carrier"> <bind-xml name="carrier" node="attribute"/> </field> <field name="departureTime"> <bind-xml name="depart" node="attribute"/> </field> <field name="arrivalTime"> <bind-xml name="arrive" node="attribute"/> </field> </class> </mapping>
书写测试类,实现Unmarshaller与Marshaller的具体用法
import java.io.*; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.xml.*; public class FlightBeanMapTest { public static void main(String[] argv) { // build a test bean FlightBean bean = new FlightBean(); bean.setCarrier("AR"); bean.setNumber(426); bean.setDepartureTime("6:23a"); bean.setArrivalTime("8:42a"); try { // write it out as XML (if not already present) Mapping map = new Mapping(); map.loadMapping("FlightBeanMapTestmapping.xml"); File file = new File("test.xml"); Writer writer = new FileWriter(file); Marshaller marshaller = new Marshaller(writer); marshaller.setMapping(map); marshaller.marshal(bean); // now restore the value and list what we get Reader reader = new FileReader(file); Unmarshaller unmarshaller = new Unmarshaller(map); FlightBean read = (FlightBean) unmarshaller.unmarshal(reader); System.out.println("Flight " + read.getCarrier() + read.getNumber() + " departing at " + read.getDepartureTime() + " and arriving at " + read.getArrivalTime()); } catch (IOException ex) { ex.printStackTrace(System.err); } catch (MarshalException ex) { ex.printStackTrace(System.err); } catch (ValidationException ex) { ex.printStackTrace(System.err); } catch (MappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }