Ignore this response - Joshua Davies's response on this topic is cleaner and simpler. Thanks Joshua!

 - Dennis

Dennis Sosnoski wrote:

Hi Bruno,

What you want can't be handled cleanly by the current JiBX code. JiBX uses order-dependent unmarshalling, matching the way most XML is structured. JiBX does include basic handling of unordered elements, but not including collections that are mixed in with other elements.

You could get around the problem by handling the collection yourself and making the child elements of <emp> unordered. To do this, you'd need to add ordered="false" to the <mapping name="emp" ...> in your binding, and also make all the child binding components optional. Then instead of the <collection> of roles, just use a <structure> and specify a set-method. Your set-method can then add the role to the set each time it's called. The drawbacks to this approach is that JiBX won't complain if any of the expected child elements are missing, and also that you'd need to making the binding input only. If you need to do both marshalling and unmarshalling you could use the current binding for the output side.

 - Dennis

Bruno De Nys wrote:

Hi,

Consider the following XML document:
<emp>
<name>John Doe</name>
<empId>03700</empId>
<role>
<dept>IT</dept>
<title>mgr</title>
</role>
<phone>123456</phone>
</emp>
It is easy to unmarshal this document to the class Employee with a field of "type" Set containing a single Role-instance:
class Employee{
private String name;
private String empId;
private String phone;
private Set roles; // set of one or more Role's
...
}
class Role{
private String dept;
private String title;
}
Once in a while, the external application which provides the XML-data also provides data for an employee with two roles with the second role appended at the end of the document:
<emp>
<name>John Doe</name>
<empId>03700</empId>
<role>
<dept>IT</dept>
<title>mgr</title>
</role>
<phone>123456</phone>
<role>
<dept>Finance</dept>
<title>mgr</title>
</role>
</emp>
In this case, JiBX fails to unmarshal because it expects a </emp>-tag after the </phone>-tag (and not the second <role>-tag). Apparently, JiBX unmarshalling only works for elements appearing in the order they are defined in the binding. When I put the second <role>-element directly behind the first <role>-element, everything works fine. Is this behaviour expected?

Or am I somewhere wrong?

Anybody has encountered the same problem? An easy way to fix this? As the information comes from an external party, it would not be easy to have them change the layout of XML messages.

I am using JiBX 1.0.1 and I use a binding in the style of:
<binding>
    <mapping name="emp"
        class="Employee">
        <value name="name" field="name" />
<value name="empId" field="empId" /> <value name="phone" field="phone" />
        <collection field="roles"
            factory="Factory.listFactory"
            item-type="Role"
            usage="optional"/>
    </mapping>
    <mapping name="roles" class="Role">
        <value name="dept" field="dept" />
        <value name="title" field="title" />
    </mapping>
</binding>

regards,
Bruno.




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to