That's the default jibx behavior (assuming that the
elements will be returned in the same order as they are defined in the
document). This is essentially a performance optimization; you can turn
this optimization off by specifying "ordered='false'" in your bindings file and
marking all subelements "optional", like this:
<binding>
<mapping name="emp" class="Employee" ordered="false">
<value name="name" field="name" usage="optional" />
<value name="empId" field="empId" usage="optional" />
<value name="phone" field="phone" usage="optional" />
<collection field="roles"
factory="Factory.listFactory"
item-type="Role"
usage="optional"/>
</mapping>
<mapping name="role" class="Role">
<value name="dept" field="dept" />
<value name="title" field="title" />
</mapping>
</binding>
<mapping name="emp" class="Employee" ordered="false">
<value name="name" field="name" usage="optional" />
<value name="empId" field="empId" usage="optional" />
<value name="phone" field="phone" usage="optional" />
<collection field="roles"
factory="Factory.listFactory"
item-type="Role"
usage="optional"/>
</mapping>
<mapping name="role" class="Role">
<value name="dept" field="dept" />
<value name="title" field="title" />
</mapping>
</binding>
Of course, this will unmarshal a little
slower.
Notice that, in addition to adding "ordered=false" &
"usage="optional" in the "emp" mapping, I also changed the "name" attribute of
the role mapping (in your example below, you had it named "roles", which doesn't
match anything in your sample XML document - it should be "role" to match the
containing element.
Also, this doesn't work correctly prior to rc0 (e.g. it
didn't work when I tried it with beta-3c) due to an internal error in the beta
versions. If you're using an older version, upgrade (as I probably ought
to myself...)
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bruno De Nys
Sent: Monday, February 20, 2006 9:47 AM
To: [email protected]
Subject: [jibx-users] Re: jibx-users digest, Vol 1 #697 - 2 msgs
Consider the following XML document:
<emp>
<name>John
Doe</name>
<empId>03700</empId>
<role>
<phone>123456</phone>
<empId>03700</empId>
<role>
<dept>IT</dept>
<title>mgr</title>
</role><title>mgr</title>
<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
...
private String empId;
private String phone;
private Set roles; // set of one or more Role's
...
}
class Role{
class Role{
private String dept;
private String title;
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>
<phone>123456</phone>
<role>
</emp><empId>03700</empId>
<role>
<dept>IT</dept>
<title>mgr</title>
</role><title>mgr</title>
<phone>123456</phone>
<role>
<dept>Finance</dept>
<title>mgr</title>
</role><title>mgr</title>
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.
