tags:

views:

434

answers:

1

I have a need to set a collection of name/value pairs inside my application - representing effectively a many-to-many map (i.e. keys can have multiple values and vice-versa), so Hashtable and Hashmap don't really work for me.

I'd like to be able to declare these via Spring in a format similar to <props>, but of course that won't work directly because Properties extends Hashtable itself.

IE, I want to be able to declare something like this:

<entry>
   <key><value>KeyOne</value></key>
   <value>ValueOne</value>
</entry>
<entry>
   <key><value>KeyOne</value></key>
   <value>ValueTwo</value>
</entry>

inside a property - but can't use <map>, <props>, and can't see a good way to use <set> or <list>. Any bright ideas?

I don't necessarily need a map-like syntax as in the above; but have that for now as I've initially implemented this code with a HashMap as the actual data structure being set by Spring. After discovering my 1-1 map is really a 1-many or even many-many, here I am.

Thanks.

+3  A: 

What about setting your collection entries as simple strings and decoding the key and value?

@Required
public void setData(String[] data) {
    // split each data element into key and value at the comma
}

<property name="data">
    <list>
        <value>key,value</value>
        <value>key,value</value>
    </list>
</property>

This is doing more work but gets you compact XML.

David Tinker
It's trivial and simple, and good enough - I like it. Thanks for the idea.
M1EK
Oh, and commas aren't a problem here; I'm actually using this data to set up a reflection scheme where I set field X of an object based on the value in field Y of another object - so commas obviously aren't valid in the strings themselves.
M1EK