xml与java技术用Castor进行数据绑定
Castor简介:
Castor是OXM的一个实现,主要完成XML文件数据与Java类实体间的映射。(实际上,Castor的功能远远不止这些,只不过这个是被人熟知和常用的罢了)。如果你现在还在使用dom和sax处理xml文档,只能说你太落伍了!!!
废话不多说,下面开始正题
功能说明:这里有一个student类,当执行createStudent时,会将一个student实体转化输出到一个XML文档中。当执行readStudent时,将会从xml文档中读取信息并生成一个student实体。
Student实体类很简单,就是平常的实体,没有任何特殊标记:
Student.java
package com.guan.castorTest.xmodel;
?
import java.util.Date;
?
public class Student {
??? private String studentName;
??? private String studentId;
??? private Date birthDay;
??? private int id;
??? public String getStudentName() {
?????? return studentName;
??? }
??? public void setStudentName(String studentName) {
?????? this.studentName = studentName;
??? }
??? public String getStudentId() {
?????? return studentId;
??? }
??? public void setStudentId(String studentId) {
?????? this.studentId = studentId;
??? }
??? public Date getBirthDay() {
?????? return birthDay;
??? }
??? public void setBirthDay(Date birthDay) {
?????? this.birthDay = birthDay;
??? }
??? public int getId() {
?????? return id;
??? }
??? public void setId(int id) {
?????? this.id = id;
??? }
}
?
需要写一个mapping,完成Student类和xml文档之间的具体映射,就是Student属性和xml文档的属性和字段之间的映射。
?
<!DOCTYPE databases PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
<!—class标签指明需要映射的类,name是这个类的类名,注意要写全哦!
Map-to只有根源素对应的类才配置这个属性
Field 类字段和xml字段之间的映射 filed中的name是类字段的名字
Bind-xml 是xml文档中对应的字段信息,name是名字,node指明是element还是attribute,默认是element -->
??? <class name="com.guan.castorTest.xmodel.Student">
?????? <map-to xml="student"/>
?????? <field name="birthDay">
?????????? <bind-xml name="birth-day" node="attribute"/>
?????? </field>
?????? <field name="id">
?????????? <bind-xml name="id" node="attribute"/>
?????? </field>
?????? <field name="studentName">
?????????? <bind-xml name="student-name"/>
?????? </field>
?????? <field name="studentId">
?????????? <bind-xml name="student-id" node="attribute"/>
?????? </field>?????????
??? </class>
</mapping>
需要注意的是,一个映射文件可以映射多个类。并且Castor可以提供一个默认的mapping(如果你不想写这个文件的话,但是一般对负责的结构,还是要写这样的mapping文件的),在默认的映射中要遵守相应的命名规则,就是说java中studentName(驼峰表示法)映射到xml中的student-name。
?
就这样我们的主要工作完成,下面是写测试,本次示例仍然使用junit。测试代码如下:
StudentTest.java
package com.guan.castorTest.xmodel;
?
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Date;
?
?
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.junit.Test;
?
public class StudentTest {
??? @Test
??? public void createStudent() throws IOException, MappingException, MarshalException, ValidationException
??? {
?????? Student student = new Student();//创建一个学生实例
?????? student.setBirthDay(new Date());
?????? student.setId(1);
?????? student.setStudentId("S05080444");
?????? student.setStudentName("张三");
??????
?????? Mapping mapping = new Mapping();//创建一个mapping,载入mapping文件
?????? mapping.loadMapping("mapping.xml");
??????
?????? File file = new File("student.xml");//输出的xml文件,以覆写方式写入
?????? Writer writer = new FileWriter(file);
?????? Marshaller marshaller = new Marshaller(writer);//编组
?????? marshaller.setMapping(mapping);//设置mapping
?????? marshaller.marshal(student);//将student输出到xml文档中
??? }
???
??? @Test
??? public void readStudent() throws IOException, MappingException, MarshalException, ValidationException
??? {
?????? Student student;
??????
?????? Mapping mapping = new Mapping();
?????? mapping.loadMapping("mapping.xml");
??????
?????? File file = new File("student.xml");//读取的文档
?????? Reader reader = new FileReader(file);
?????? Unmarshaller umarshaller = new Unmarshaller(mapping);//解组
?????? student = (Student) umarshaller.unmarshal(reader);//得到实体
??????
?????? System.out.println(student.getId());//输出实体
?????? System.out.println(student.getStudentId());
?????? System.out.println(student.getStudentName());
?????? System.out.println(student.getBirthDay());
??????
??? }
}
?
通过junit test 执行createStudent得到下面结果:
Log输出:
2010-8-4 22:58:59 org.exolab.castor.mapping.Mapping setBaseURL
信息: mapping.xml is not a URL, trying to convert it to a file URL
2010-8-4 22:58:59 org.exolab.castor.mapping.Mapping loadMapping
信息: Loading mapping descriptors from mapping.xml
?
获得的xml中的信息:
<?xml version="1.0" encoding="UTF-8"?>
<student birth-day="2010-08-04T22:58:59.281+08:00" id="1" student-id="S05080444">
<student-name>张三</student-name>
</student>
?
执行readStudent得到:
信息: mapping.xml is not a URL, trying to convert it to a file URL
2010-8-4 23:00:41 org.exolab.castor.mapping.Mapping loadMapping
信息: Loading mapping descriptors from mapping.xml
1
S05080444
张三
Wed Aug 04 22:58:59 CST 2010
更多复杂操作可以参考:http://hi.baidu.com/higuang/blog/item/7d63cbc4de9fecc639db4904.html
?
需要的jar包:
castor-1.3.jar
castor-1.3-core.jar
commons-logging.jar