我有两个包,并且两个包在我的项目中都有一些同名的类。我已经使用 spring oxm 成功地为同名的类创建了 XML 文件,但是当我尝试从 XML 转换为类时,它显示以下错误:
java.lang.ClassCastException: com.tmk.loanprocessing.core.template.overdraft.Recommendation cannot be cast to com.tmk.loanprocessing.template.autoloan.Recommendation
用于 XML 编组和解组的 Spring 配置在这里:
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="targetClasses">
<array>
<!-- For Overdraft Loan Entity -->
<value>com.tmk.loanprocessing.core.template.overdraft.CustomerInfo</value>
<value>com.tmk.loanprocessing.core.template.overdraft.CompanyInformation</value>
<value>com.tmk.loanprocessing.core.template.overdraft.SiteVisit</value>
<value>com.tmk.loanprocessing.core.template.overdraft.Proposal</value>
<value>com.tmk.loanprocessing.core.template.overdraft.Security</value>
<value>com.tmk.loanprocessing.core.template.overdraft.CICL</value>
<value>com.tmk.loanprocessing.core.template.overdraft.Financials</value>
<value>com.tmk.loanprocessing.core.template.overdraft.Recommendation</value>
<value>com.tmk.loanprocessing.core.template.overdraft.SectorInfo</value>
<!-- For Auto Loan Entity -->
<value>com.tmk.loanprocessing.template.autoloan.BasicInfo</value>
<value>com.tmk.loanprocessing.template.autoloan.Company</value>
<value>com.tmk.loanprocessing.template.autoloan.SiteVisit</value>
<value>com.tmk.loanprocessing.template.autoloan.Security</value>
<value>com.tmk.loanprocessing.template.autoloan.Financial</value>
<value>com.tmk.loanprocessing.template.autoloan.Recommendation</value>
</array>
</property>
</bean>
编组和解组代码是:
public void convertFromObjectToXML(Object object,Object xmlPath){
File file = null;
FileOutputStream fos = null;
String className = object.getClass().getSimpleName();
try{
file = new File((String)xmlPath);
file.mkdirs();
fos = new FileOutputStream(xmlPath+className+".xml");
marshaller.marshal(object, new StreamResult(fos));
}catch(Exception ex){
System.out.println("Error occured while marshalling");
ex.printStackTrace();
}
}
public Object convertFromXMLToObject(File xmlPath){
FileInputStream fileInputStream = null;
try{
fileInputStream = new FileInputStream(xmlPath);
return unmarshaller.unmarshal(new StreamSource(fileInputStream));
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
我怎样才能摆脱这个错误?请问有什么建议吗?