1. Castor是什么
Castor是一种将Java对象和XML自动绑定的开源软件. 它可以在Java对象,XML文本,SQL数据表以及LDAP目录之间绑定.
²网址: http://www.castor.org/
2. Castor使用
²Java对象指的是具有setX和getX方法的JavaBean,将Castor用于JavaBean具体使用方法如下:
Ø缺省用法: 缺省用法指的是没有使用映射格式描述文件时的用法
import java.io.*;
import org.exolab.castor.xml.*;
public class Test
{
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
File file = new File("test.xml");
Writer writer = new FileWriter(file);
Marshaller.marshal(bean, writer);
// now restore the value and list what we get
Reader reader = new FileReader(file);
FlightBean read = (FlightBean)
Unmarshaller.unmarshal(FlightBean.class, 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);
}
}
}
Ø 标准用法:
import java.io.*;
import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.*;
public class Test
{
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
Mapping map=new Mapping();
map.loadMapping("mapping.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 ex) {
ex.printStackTrace(System.err);
}
}
}
Ø :缺省用法生成的XML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<arrival-time>8:42a</arrival-time>
<departure-time>6:23</departure-time>
<carrier>AR</carrier>
也就是说
•对于具有基本类型值的属性创建元素的一个属性(本例中只有 number 属性通过 getNumber() 方法公开为 int 值)。
•对于每个具有对象类型值的属性创建根元素的一个子元素(本例中的所有其他属性,因为它们是字符串)。
Ø标准用法生成的XML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<flight carrier="AR" depart="6:23a" arrive="8:42a" number="426"/>
这一般是我们期望的,只所以生成我们所期望的格式,是因为我们使用了映射格式描述文件。它也是XML文件:
<!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="castortest.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>• 注意class标签里name应该是带有包名的JavaBean类全名
•map-to xml="flight"指XML文件的根节点,区分大小写
•auto-complete=”false”或没有auto-complete属性时number属性会没有