不同包中两个同名类的相同XML文件名具有类强制转换异常

时间:2017-06-22 09:44:44

标签: java spring spring-oxm

我有两个包,两个包都有一些在我的项目中具有相同名称的类。我使用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;
}

我怎么能摆脱这个错误? 有什么建议吗?

0 个答案:

没有答案
相关问题