Well, this was certainly an interesting problem... perhaps Mr. Sosnoski
should start posting a "Jibx brain-teaser" on the sourceforge home page
each day with this sort of question.
I wasn't able to solve your problem without using abstract mappings, but
if you can deal with the introduction of a parent class for
"update-user" & "create-user", the binding file below works correctly:
<binding direction="input">
<mapping name="request-set" class="com.sample.RequestSet">
<collection field="requests">
<structure name="request">
<structure type="com.sample.Request" />
</structure>
</collection>
</mapping>
<mapping class="com.sample.Request" abstract="true">
<value name="id" style="attribute" field="id" />
</mapping>
<mapping name="create-user" class="com.sample.CreateUser"
extends="com.sample.Request">
<structure map-as="com.sample.Request" />
<value name="name" style="element" field="name" />
<value name="age" style="element" field="age" />
</mapping>
<mapping name="update-user" class="com.sample.UpdateUser"
extends="com.sample.Request">
<structure map-as="com.sample.Request" />
<value name="status" style="element" field="status" />
</mapping>
</binding>
With your given XML (I'll assume that lines 11 & 16 were typos;
otherwise the XML is malformed). Since you weren't picky about the
class structure, here are the classes I used:
public class RequestSet
{
private ArrayList requests;
}
public class Request
{
protected String id;
}
public class CreateUser
extends Request
{
private String name;
private String age;
}
public class UpdateUser
extends Request
{
private int status;
}
If you have more stringent requirements as to the target object
structure, reply back and I'll see if I can come up with anything else.
I'd be curious to see if any of the true jibx gurus on this list can
solve this problem without using abstract mappings... although their use
flows logically from this problem, from a flexibility standpoint, I'd
like to know if it can be done.
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
[EMAIL PROTECTED]
Sent: Friday, March 04, 2005 2:00 PM
To: [email protected]
Subject: [jibx-users] How can I bind this XML<->Class ...?
Hello,
I have spend a couple of hours trying to figure out how to bind the
following XML into classes:
<request-set>
<request>
<create-user id="3">
<name>cedric</name>
<age>35</age>
</create-user>
</request>
<request>
<update-user id="3">
<status>1</status>
</create-user>
</request>
<request>
<update-user id="5">
<status>0</status>
</create-user>
</request>
</request-set>
...etc....
The problem here is that the element <request> may have different forms:
it can contain one and only on element <create-user> or <update-user>
.etc...
You can see that <request-set> can contain an unlimited amount of
<request> elements: This could be bind to a <collection> in the JiBX
bind file, but the problem is that the collection (ArrayList in the
underlaying class) in this case needs to contain different objects
(<create-user> or <update-user>) ... I am getting sooooooo confused
...
Some help on how to map my little XML would be greatly appreciated ...
Thanks!
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_ide95&alloc_id396&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users