Search code examples
javamappinghierarchyextendscastor

Castor: How to map a Java hierarchy


I have this Java-Castor issue while mapping a hierarchy. Thanks for your time.

I have this XML file:

<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>

This Java hierarchy:

public class RentedWSResult 
{
    private boolean success;

    private List<RentedWSResultError> errors;
}

public class GetCompaniesRentedWSResult extends RentedWSResult 
{
    private List<RentedCompany> rentedCompanies;
}

And these Castor Mappings:

<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>

It's not working, getting this error:

Unable to find FieldDescriptor for 'COMPANIES' in ClassDescriptor of RESULT

Solution

  • There is no relationship between your classes in the mapping file.

    With the info you provided I put together a mapping that works. It assumes that your RentedWSResult is your container class for your other elements.

    <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>