You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by RODRIGUEZ Eric <er...@cetelem.be> on 2006/03/20 10:04:11 UTC

Betwixt and collections

Hi all,

I'm sorry if this may seem to you a classic (dummy) question but I get some difficulties to get working betwixt with collections.
Basically, my problem is with List  I got a userlist with "boys" and "girls" list, and I want to be populated in the right way.
I get lost in the documentation to find a example on how to handle correctly the "addDefaults" or "updater" settings.
What should I use to ensure that boys entries are added to boys List (and girls to girls List).

Sorry if this may seem too simple but I tried several betwixt-config files and .betwixt but I can't get this working properly.

Thanks for any help or comments or links.

Eric Rodriguez


Here are my test classes:
Betwixt Write:
        UserList userList = Factory.getData();
        FileWriter outputWriter = new FileWriter(new File("users.xml"));
        outputWriter.write("<?xml version='1.0' ?>");

        BeanWriter beanWriter = new BeanWriter(outputWriter);
        beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
        beanWriter.getBindingConfiguration().setMapIDs(false);
        beanWriter.enablePrettyPrint();

        beanWriter.write("users", userList);

        outputWriter.close();

Betwixt XML out:
<?xml version='1.0' ?>  <users>
    <boys>
      <User>
        <name>Boy 1</name>
      </User>
      <User>
        <name>Boy 2</name>
      </User>
    </boys>
    <girls>
      <User>
        <name>Girl 1</name>
      </User>
      <User>
        <name>Girl 2</name>
      </User>
    </girls>
    <userListName>User List</userListName>
  </users>


Betwixt Read:
        FileReader xmlReader = new FileReader(new File("users.xml"));
        BeanReader beanReader = new BeanReader();

        beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
        beanReader.getBindingConfiguration().setMapIDs(false);
        
        beanReader.registerBeanClass("users", UserList.class);
        UserList userList = (UserList) beanReader.parse(xmlReader);



UserList
public class UserList {

    private String userListName;

    private ArrayList girls;

    private ArrayList boys;

    public UserList() {
        this.girls = new ArrayList();
        this.boys = new ArrayList();
    }

    public ArrayList getBoys() {
        return boys;
    }

    public void setBoys(ArrayList boys) {
        this.boys = boys;
    }

    public ArrayList getGirls() {
        return girls;
    }

    public void setGirls(ArrayList girls) {
        this.girls = girls;
    }

    public String getUserArrayListName() {
        return userListName;
    }

    public void setUserArrayListName(String userListName) {
        this.userListName = userListName;
    }
    
    public void addBoy(User user) {
        this.boys.add(user);
    }
    public void addGirl(User user) {
        this.girls.add(user);
    }

}

User
public class User {

    private String name;

    public User() {
        super();
    }

    public User(String userName) {
        this.name = userName;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


Re: Betwixt and collections

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Mon, 2006-03-20 at 10:04 +0100, RODRIGUEZ Eric wrote: 
> Hi all,

hi eric

> I'm sorry if this may seem to you a classic (dummy) question but I get some difficulties to get working betwixt with collections.
> Basically, my problem is with List  I got a userlist with "boys" and "girls" list, and I want to be populated in the right way.
> I get lost in the documentation to find a example on how to handle correctly the "addDefaults" or "updater" settings.

it's worth bearing in mind that the documentation on the website
represents the latest code. a lot of work has been put in since the last
release and i recommend updating to the latest code (build from the
repository or grab a recent nightly) if you have any problems. 

> What should I use to ensure that boys entries are added to boys List (and girls to girls List).
> 
> Sorry if this may seem too simple but I tried several betwixt-config files and .betwixt but I can't get this working properly.

if you have flexibility over your xml format then this will work without any mapping file if you are willing to make a few changes the format.

the wrapping element for each object in the collection should match the property name. so if you use:

> Betwixt XML out:
> <?xml version='1.0' ?>  <users>
>     <boys>
>       <boy>
>         <name>Boy 1</name>
>       </boy>
>       <boy>
>         <name>Boy 2</name>
>       </boy>
>     </boys>
>     <girls>
>       <girl>
>         <name>Girl 1</name>
>       </girl>
>       <girl>
>         <name>Girl 2</name>
>       </girl>
>     </girls>
>     <userListName>User List</userListName>
>   </users>

this works for me (using the latest code - you may need to update).

note that 'userListName' does not match the name of your bean property. the easiest way to make this mapping work is to either change the name of your property or of the element.

if the xml format is fixed then this makes things a little more difficult and you'll need a dot betwixt file for UserList. let me know if this is the case and i'll try to give you some more help.

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org