<!DOCTYPE databases PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd"><mapping>
<class name="com.lczmsoft.bpe.busi.RoleBox" auto-complete="false">
<field name="EML_ID_LIST" type="com.a.Role"
collection="collection"/>
</class> <class name = "com.a.BpeRole">
<field name="EML_ID"/>
</class>
</mapping>这是一个mapping文件
不知道你的类结构,所以只能写一个框架
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd"><mapping>
<class name="com.lczmsoft.bpe.busi.RoleBox" auto-complete="false">
<field name="EML_ID_LIST" type="com.a.Role"
collection="collection"/>
</class> <class name = "com.a.BpeRole">
<field name="EML_ID"/>
</class>
</mapping>这是一个mapping文件
不知道你的类结构,所以只能写一个框架
castor的文档上有很详细的说明的.
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd"><mapping>
<class name="demo.Workers" auto-complete="false">
<map-to xml="Workers"/>
<field name="employees" type="demo.Employee"
collection="collection">
<bind-xml name="EML_ID_LIST"/>
</field>
</class>
<class name = "demo.Employee">
<field name="empId">
<bind-xml name="EML_ID" node="element"/>
</field>
</class>
</mapping>
package demo;import java.util.ArrayList;
import java.util.List;public class Workers {
private List employees =new ArrayList(); public void setEmployees(List empls) {
this.employees = empls;
} public List getEmployees() {
return employees;
}}package demo;public class Employee {
private String empId;public void setEmpId(String empId) {
this.empId = empId;
}public String getEmpId() {
return empId;
}
}下边是解析方法: Workers emp = null;
Mapping map = new Mapping();
try {
map.loadMapping(this.getClass().getResource(
"/config/Role.mapping.xml").getFile());
Reader reader = new FileReader(new File(this.getClass()
.getResource("/config/Role.define.xml").getFile()));
Unmarshaller unmarshaller;
unmarshaller = new Unmarshaller(map);
emp = (Workers) unmarshaller.unmarshal(reader);
if (emp != null) {
List list = emp.getEmployees();
for (int i = 0; i < list.size(); i++) {
log.debug(list.get(i));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
你的原来的xml数据要写成下边的样式<?xml version="1.0" encoding="gb2312"?>
<Workers>
<EML_ID_LIST>
<EML_ID>479291230600000028</EML_ID>
</EML_ID_LIST>
<EML_ID_LIST>
<EML_ID>479291230600000029</EML_ID>
</EML_ID_LIST>
</Workers>