Castor(http://castor.exolab..org/)是一种将Java对象和XML自动绑定的开源软件。
Castor可以完成Java和XML的相互转换
第一步:新建javabean(PosPoilcyHistoryChangeResponse )
public class PosPoilcyHistoryChangeResponse {
//返回码
private String RetCode = "";
//返回信息
private String RetMsg = "";
private String PolicyNumber = "";
//保全业务笔数
private String preservationNumber;
//保全信息
private List<PreservationtDetail> prsList = new ArrayList<PreservationtDetail>();
//保单生效复效次数
private String InsPolicyEffRinst;
//保单生效复效信息
private List<InsPolicyDtDetail> InsPolicyDtList= new ArrayList<InsPolicyDtDetail>();
//保单保额变动次数
private String InsPolicyCvrChgNum;
//保单保额变动信息
private List<InsPolicyAplyDetail> InsPolicyAplyList = new ArrayList<InsPolicyAplyDetail>();
//理赔记录数
private String SetlOfClmsRcrdNum;
//理赔信息
private List<SetlOfClmsDetail> SetlOfClmsList= new ArrayList<SetlOfClmsDetail>();
public String getRetCode() {
return RetCode;
}
public void setRetCode(String retCode) {
RetCode = retCode;
}
public String getRetMsg() {
return RetMsg;
}
public void setRetMsg(String retMsg) {
RetMsg = retMsg;
}
public String getPolicyNumber() {
return PolicyNumber;
}
public void setPolicyNumber(String policyNumber) {
PolicyNumber = policyNumber;
}
public String getPreservationNumber() {
return preservationNumber;
}
public void setPreservationNumber(String preservationNumber) {
this.preservationNumber = preservationNumber;
}
public List<PreservationtDetail> getPrsList() {
return prsList;
}
public void setPrsList(List<PreservationtDetail> prsList) {
this.prsList = prsList;
}
public String getInsPolicyEffRinst() {
return InsPolicyEffRinst;
}
public void setInsPolicyEffRinst(String insPolicyEffRinst) {
InsPolicyEffRinst = insPolicyEffRinst;
}
public List<InsPolicyDtDetail> getInsPolicyDtList() {
return InsPolicyDtList;
}
public void setInsPolicyDtList(List<InsPolicyDtDetail> insPolicyDtList) {
InsPolicyDtList = insPolicyDtList;
}
public String getInsPolicyCvrChgNum() {
return InsPolicyCvrChgNum;
}
public void setInsPolicyCvrChgNum(String insPolicyCvrChgNum) {
InsPolicyCvrChgNum = insPolicyCvrChgNum;
}
public List<InsPolicyAplyDetail> getInsPolicyAplyList() {
return InsPolicyAplyList;
}
public void setInsPolicyAplyList(List<InsPolicyAplyDetail> insPolicyAplyList) {
InsPolicyAplyList = insPolicyAplyList;
}
public String getSetlOfClmsRcrdNum() {
return SetlOfClmsRcrdNum;
}
public void setSetlOfClmsRcrdNum(String setlOfClmsRcrdNum) {
SetlOfClmsRcrdNum = setlOfClmsRcrdNum;
}
public List<SetlOfClmsDetail> getSetlOfClmsList() {
return SetlOfClmsList;
}
public void setSetlOfClmsList(List<SetlOfClmsDetail> setlOfClmsList) {
SetlOfClmsList = setlOfClmsList;
}
}
第二步:将javaBean(PosPoilcyHistoryChangeResponse) 配置到castorMapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
</class>
<class name="com.siebre.itx.aia.PosPoilcyHistoryChangeResponse">
<map-to xml="ResponseDTO" />
<field name="RetCode"><bind-xml name="RetCode" /></field>
<field name="RetMsg"><bind-xml name="RetMsg" /></field>
<field name="PolicyNumber"><bind-xml name="PolicyNumber" /></field>
<field name="preservationNumber"><bind-xml name="preservationNumber" /></field>
<field name="InsPolicyEffRinst"><bind-xml name="InsPolicyEffRinst" /></field>
<field name="InsPolicyCvrChgNum"><bind-xml name="InsPolicyCvrChgNum" /></field>
<field name="SetlOfClmsRcrdNum"><bind-xml name="SetlOfClmsRcrdNum" /></field>
</class>
</mapping>
第三步:实现xml的解组
public static void main(String[] args) {
//创建CastorMarshaller
CastorMarshaller castorMarshaller = new CastorMarshaller();
castorMarshaller.setMappingLocation(new ClassPathResource("AiaCastorMapping.xml", PosPoilcyHistoryChangeResponse.class));
castorMarshaller.setIgnoreExtraElements(true);
castorMarshaller.setIgnoreExtraAttributes(true);
castorMarshaller.afterPropertiesSet();
//读取要转换成javabean的xml
FileInputStream input = new FileInputStream("response.xml")
int length = input.available();
byte[] buffer = new byte[length];
input.read(buffer, 0, length);
String responseXML = new String(buffer);
//将xml解组成对象
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new StringReader(responseXML));
PosPoilcyHistoryChangeResponse nposbancaResDTO = (PosPoilcyHistoryChangeResponse) castorMarshaller.unmarshal(new DocumentSource(document));
}
第四步:实现xml的编组
public static void main(String[] args) {
CastorMarshaller castorMarshaller = getCastorMarshaller();
//创建javaBean对象,并设值
PosPoilcyHistoryChangeResponse nposbancaResDTO = new PosPoilcyHistoryChangeResponseImpl();
nposbancaResDTO.setRetCode(“aaa");
nposbancaResDTO.setRetMes(“bbbb");
DOMResult domResult = (DOMResult) new DomResultFactory().createResult(null);
castorMarshaller.marshal(nposbancaResDTO , domResult);
DOMReader domReader = new DOMReader();
Document documentOut = domReader.read((org.w3c.dom.Document) domResult.getNode());
//将javaBean编组成requestXMl
StringWriter writer = new StringWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, format);
xmlWriter.write(documentOut);
String requestXMl = writer.toString();
}