我在映射层次结构时遇到了 Java-Castor 问题。感谢您抽出时间。
我有这个 XML 文件:
<RESULT>
<RESULTCODE>OK</RESULTCODE>
<ERRORS />
<COMPANIES>
<COMPANY VD="107795641" NAME="COMPANYA"
RATING="" CIF="ABCD3435" ID="7671" NUM="0" />
<COMPANY VD="102167561" NAME="COMPANYB"
RATING="" CIF="ABCD1234" ID="6642" NUM="1" />
</COMPANIES>
</RESULT>
这个 Java 层次结构:
public class RentedWSResult
{
private boolean success;
private List<RentedWSResultError> errors;
}
public class GetCompaniesRentedWSResult extends RentedWSResult
{
private List<RentedCompany> rentedCompanies;
}
还有这些 Castor 映射:
<mapping>
<class name="RentedWSResult">
<map-to xml="RESULT" />
<field name="success" type="string" handler="BooleanStringHandler">
<bind-xml name="RESULTCODE" />
</field>
<field name="errors" type="RentedWSResultError" collection="arraylist">
<bind-xml name="ERROR" location="ERRORS" />
</field>
</class>
</mapping>
<mapping>
<include href="RentedWSResultMarshallConfig.xml"/>
<class name="GetCompaniesRentedWSResult" >
<field name="rentedCompanies" type="RentedCompany" collection="arraylist">
<bind-xml name="COMPANY" location="COMPANIES" />
</field>
</class>
</mapping>
它不起作用,出现此错误:
Unable to find FieldDescriptor for 'COMPANIES' in ClassDescriptor of RESULT
最佳答案
映射文件中的类之间没有关系。
根据您提供的信息,我整理了一个有效的映射。它假定您的 RentedWSResult 是其他元素的容器类。
<mapping>
<include href="GetCompaniesRentedWSResultBinding.xml"/>
<class name="RentedWSResult">
<map-to xml="RESULT" />
<field name="success" type="string">
<bind-xml name="RESULTCODE" />
</field>
<field name="rentedCompanies" type="GetCompaniesRentedWSResult">
<bind-xml name="COMPANIES" />
</field>
</class>
</mapping>
<mapping>
<class name="GetCompaniesRentedWSResult" >
<field name="rentedCompanies" type="RentedCompany" collection="arraylist">
<bind-xml name="COMPANY" />
</field>
</class>
<class name="RentedCompany" >
<field name="vd" type="string">
<bind-xml name="vd" node="attribute" />
</field>
<field name="name" type="string">
<bind-xml name="name" node="attribute"/>
</field>
<field name="rating" type="string">
<bind-xml name="rating" node="attribute"/>
</field>
</class>
</mapping>
关于java - Castor:如何映射 Java 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657972/