Hello,
I'm trying to produce an xml code from a map with multiple values.
I'm using the map class org.apache.commons.collections.map.MultiValueMap which
allows multiple values for one key.
I would like to produce something like that :
<Info name="key1">value1</Info>
<Info name="key1">value2</Info>
<Info name="key2">value2</Info>
<Info name="keyN">valueN</Info>
But the best I could have is :
<Info name="key1">value1</Info>
<Info>value2</Info>
<Info name="key2">value2</Info>
<Info name="keyN">valueN</Info>
I'm using the following mapping (with MapItem) :
<class name="org.exolab.castor.mapping.MapItem">
<map-to xml="Info" />
<field name="key" type="string" >
<bind-xml name="name" location="Info" node="attribute" />
</field>
<field name="value" type="string" collection="arraylist">
<bind-xml name="Info" node="element" />
</field>
</class>
Another solution is to use a handler which allows me to get the MultiValueMap
object then i construct the xml code (see the following code) :
public class MultiValueMapHandler extends GeneralizedFieldHandler {
public MultiValueMapHandler() {
setCollectionIteration(false);
}
@SuppressWarnings("unchecked")
@Override
public Class getFieldType() {
return MultiValueMap.class;
}
@Override
public Object convertUponGet(Object source) {
MultiValueMap map = (MultiValueMap) source;
StringBuilder xml = new StringBuilder();
for (Object key : map.keySet()) {
for (Object value : map.getCollection(key)) {
xml.append("<Info
name=\""+key.toString()+"\">").append(value.toString()).append("<Info/>");
}
}
return xml.toString();
}
@Override
public Object convertUponSet(Object source) {
// There is no need to implement this method
return null;
}
}
And i got the following xml code :
<Request type="keep-put" version="1">
<ModuleId>KEEPClient</ModuleId>
<RequestId>REQUEST001</RequestId>
<Document>c2FtcGxlIGRvY3VtZW50</Document>
<DocumentInfo>
<Info name="key1">value1<Info/>
<Info name="key1">value2<Info/>
<Info name="key2">value2<Info/>
<Info name="keyN">valueN<Info/>
</DocumentInfo>
</Request>
As you could see the '<' and '>' is not escaped and i can't produce the right
xml code.
Does someone have any idea to solve this problem?
Thanks you in advance.
Maghen.