I have a Map<String, String>
.
The first idea everyone has is to convert it to a List<Pair<String,String>>
(Pair
being a custom class).
I've tried a @XmlAdapter
like this:
public class MapPropertiesAdapter extends XmlAdapter<List<Property>, Map<String,String>> { ... }
But Eclipse MOXy, the JAXB impl I use, ended up with a ClassCastException
- "can't convert HashMap to Collection".
Is this conversion supported by JAXB? Or did I overlook some documentation part which explains why it isn't?
PS: I wanted to get XML like this:
<properties>
<property name="protocol"/>
<property name="marshaller"/>
<property name="unmarshaller"/>
<property name="timeout"/>
...
</properties>
I got it, only had to use an intermediate class. Also described at Handle NPE in XMLCompositeObjectMappingNodeValue.marshalSingleValue( XMLCompositeObjectMappingNodeValue.java:161)
Instead of adapting the
Map
to aList
, you should adapt it to an object that has aList
property.XmlAdapter (MapPropertiesAdapter)
Domain Model (Root)
Below is a model object with a
Map
property. The@XmlJavaTypeAdapter
annotation is used to specify theXmlAdapter
.Demo
input.xml/Output