I tried to do in way that is in below link
How to convert XML to java.util.Map and vice versa
But this is not going all over the Map.
Please Advise!
I tried to do in way that is in below link
How to convert XML to java.util.Map and vice versa
But this is not going all over the Map.
Please Advise!
You need to use XmlAdapter
and override marshal
and unmarshal
method.
Here is example code show how to convert Map<String,Boolean>
to XML and vice versa:
public class MapAdapter extends XmlAdapter<MapElements[], Map<String, Boolean>> {
public MapAdapter() {
}
@Override
public MapElements[] marshal(Map<String, Boolean> v) throws Exception {
MapElements[] elements = new MapElements[v.size()];
int i = 0;
for (Map.Entry<String, Boolean> entry : v.entrySet()) {
elements[i++] = new MapElements(entry.getKey(), entry.getValue());
}
return elements;
}
@Override
public Map<String, Boolean> unmarshal(MapElements[] v) throws Exception {
Map<String, Boolean> map = new HashMap<>();
for (MapElements element : v) {
map.put(element.key, element.value);
}
return map;
}
}
class MapElements {
@XmlElement
public String key;
@XmlElement
public Boolean value;
public MapElements() {
}
public MapElements(String key, Boolean value) {
this.key = key;
this.value = value;
}
}
and use @XmlJavaTypeAdapter
annotation on Map<String,Boolean>
field as follow
@XmlJavaTypeAdapter(MapAdapter.class)
private Map<String, Boolean> map