Search code examples
xmlfilemappingcastor

Castor - Unmarshalling a simple repeating group


I'm going slowly mad over this. I'm trying to unmarshall an XML document like this:

<GetDeadlineOffset>
    <deadlineCode>DC1</deadlineCode>
    <parameters>
      <parameter name="P1">Param 1</parameter>
      <parameter name="P2">Param 2</parameter>
    </parameters>
</GetDeadlineOffset>

I have a pair of simple POJOs, GetDeadlineOffsetRequest and Parameter, which look like this:

public class GetDeadlineOffsetRequest {

    private String deadlineCode = null;
    private List<Parameter> parmList = new ArrayList<Parameter>();


    public GetDeadlineOffsetRequest() {
        // Do nothing
    }

    public String getDeadlineCode(){
        return this.deadlineCode;
    }

    public void setDeadlineCode(String deadlineCode){
        this.deadlineCode = deadlineCode;
    }

    public List<Parameter> getParameters() {
        return parmList;
    }

    public void setParameters(List<Parameter> parmList) {
        this.parmList = parmList;
    }

} 

and

public class Parameter {

    private String name = null;
    private String value = null;

    public Parameter() {
        // Do Nothing
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

I'm using a mapping file but can't seem to get those tags unmarshalled into my Parameter list.

This is my latest attempt at a mapping:

<class name="mypkg.GetDeadlineOffsetRequest">
    <map-to xml="GetDeadlineOffset"/>
    <field name="DeadlineCode" type="java.lang.String">
        <bind-xml name="deadlineCode" node="element" />
    </field>
    <field name="Parameters" type="mypkg.Parameter" collection="collection">
        <bind-xml name="parameters" node="element" />
    </field>    
</class>
<class name="mypkg.Parameter">
    <map-to ns-uri="http://services.blah.com/AMM/Deadline/v1"/>
    <field name="Value" type="java.lang.String">
        <bind-xml name="parameter" node="element" /> 
    </field>
    <field name="Name" type="java.lang.String">
        <bind-xml name="name" node="attribute" location="parameter" />  
    </field>
</class>

The problem seems to revolve around the fact that <parameters> is the container element and <parameter> is the repeatable element. The mapping above is telling Castor that <parameters> is repeatable.

I've been trying all sorts of combinations of mappings for a couple of days now and I think I've worked my way into a corner!

Can anyone see what I'm doing wrong?

I'm using Castor XML 1.3.2.


Solution

  • I've managed to have some success with the following mapping:

    <class name="myPkg.GetDeadlineOffsetRequest">
        <map-to xml="GetDeadlineOffset"/>
        <field name="DeadlineCode">
            <bind-xml name="deadlineCode" />
        </field>
        <field name="parameters" type="myPkg.Parameter" collection="collection">
            <bind-xml name="parameter" location="parameters">
                <class name="myPkg.Parameter">
                    <field name="Name">
                        <bind-xml name="name" node="attribute" />
                    </field>
                    <field name="Value">
                        <bind-xml node="text"/>
                    </field>
                </class>
            </bind-xml>
        </field>        
    </class>
    

    The trick was to use location="parameters" and node="text" on a couple of the bind-xml elements. I've successfully marshalled and unmarshalled using this mapping.