How to map collections with castor?

F

frank_ratzlow

Hi folks,

I have a class structure similar to the following:

====================================
class AddressBook {
/* Collection<User> */
Collection users;

public void setUsers(Collection users) {
this.users = users;
}

public Collection getUsers() {
return this.users;
}
}

class User {
String firstname;
String lastname;

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getFirstname() {
return this.firstname;
}

public String getLastname() {
return this.lastname;
}
}
====================================

and a castor mapping file:
====================================
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version
1.0//EN"
"http://castor.exolab.org/mapping.dtd">

<mapping>
<class name="AddressBook">
<map-to xml="addressbook"/>
<field name="users"
type="User"
collection="collection">
</field>
</class>

<class name="User">
<map-to xml="user"/>
<field name="firstname"
type="string">
<bind-xml name="firstname"/>
</field>
<field name="lastname"
type="string">
<bind-xml name="lastname"/>
</field>
</class>
====================================

When i unmarshall from such a xml fragment:

====================================
<addressbook>
<users>
<user>
<firstname>Peter<firstname>
<firstname>Miller<firstname>
<user>
<user>
<firstname>Mary<firstname>
<firstname>Lu<firstname>
<user>
<user>
<firstname>Jonny<firstname>
<firstname>Hot<firstname>
<user>
</users>
<addressbook>
====================================

the resulting object graph is absolutely as expected, means my user
collection contains a bunch of users.
But when I marshall it back to XML than I receive:
====================================
<?xml version="1.0" encoding="UTF-8"?>
<addressbook
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:AddressBook">
<users xsi:type="java:User">
<firstname>Peter<firstname>
<firstname>Miller<firstname>
</users>
<users xsi:type="java:User">
<firstname>Mary<firstname>
<firstname>Lu<firstname>
</users>
<users xsi:type="java:User">
<firstname>Jonny<firstname>
<firstname>Hot<firstname>
</user>
<addressbook>
====================================

Is there a way to tell the mapping to put objects from collections in
subelements of an enclosing XML-collection element?
Just would like to see the output matching the input, with no "xsi"
etc. and appropriate mapping of collections.

TIA

Frank
(e-mail address removed)

P.S. JDK 1.4.1_05, Castor 0.9.6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top