<root> <user login="kkkkkk"> <group>admin</group> <group>user</group> ... </user> <user login="ddddd"> ... </user> ... </root>
This just lists the users, but it's fairly easy to construct the groups from the user information when unmarshalling (if you use some of the extension methods in non-obvious ways). JiBX doesn't handle sets directly, but you can use an iter-method for marshalling and an add-method for unmarshalling, so add these to your User class:
private Iterator groupIterator() { return groups.iterator(); } private void addGroup(Object group) { ((Group)group).addUser(this); groups.add(group); }
Then implement the group handling with an added HashMap and "deserializer" method (which only creates a new instance if one with that name does not already exist) in Group, along with a "serializer" that just returns the group name:
private static HashMap groupNameMap = new HashMap();
private Group(String name) { this.name = name; users = new Set(); }
public static Group groupForName(String name) {
// synchronize this whole block on the hashmap if you need to allow for multithreading
Group group = (Group)groupNameMap.get(name);
if (group == null) {
group = new Group(name);
s_groupNameMap.put(name, group);
}
return group;
}
public static String getName(Group group) {
return group.name;
}
Your binding would then need to look something like this (ignoring the containing root part, which should be mapped to whatever you want to be a collection of users):
<binding>
<mapping name="root" ...>
<collection ...> <!-- no name on the collection in this case -->
<structure name="user" type="User">
<value style="attribute" name="login" field="login"/>
<collection iter-method="groupIterator" add-method="addGroup">
<value name="group" type="Group" serializer="Group.getName" deserializer="Group.groupForName"/>
</collection>
</structure>
</collection>
</mapping>
</binding>
I haven't actually tried all this out, but I think it should work. If you give it a try let me know - I'll add this to the Wiki, once you've verified it (I won't ask you to add this yourself, as I normally would, because of the complexity of the issues involved).
If you actually have more information you want to associate with the groups (beyond just the name and users), you could instead use an XML representation that includes both groups and users:
<root> <group name="admin"> ... </group> ... <user login="kkkkkk"> <group>admin</group> <group>user</group> ... </user> <user login="ddddd"> ... </user> ... </root>
In this case you could use the <group> name attributes as ID values and the references within the <user>s as IDREFs. This should actually be more obvious to handle - see the section of the tutorial on working with ID/IDREFs for collections at http://jibx.sourceforge.net/tutorial/binding-collects.html#ids
- Dennis
michael simons wrote:
Hello,
Might be an absolute newbie wuestion but I didn't see how to do it yet.
We have two classes, e.g.:
class User { String login; // unique Set groups; // groups this user belongs to }
class Group { String name; // unique Set users; // Users that belong to this group }
What kind of binding do I have to use to be able to un-/marshal instances of these classes?
Regards, Michael
------------------------------------------------------- This SF.Net email is sponsored by: SourceForge.net Broadband Sign-up now for SourceForge Broadband and get the fastest 6.0/768 connection for only $19.95/mo for the first 3 months! http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click _______________________________________________ jibx-users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jibx-users