0

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!

Community
  • 1
  • 1
ERP
  • 41
  • 1
  • 1
  • 10
  • I advise to not write such unclear / unspecific / broad questions. At least limit the question to one xml API and include a example with input/desired output. – fabian Feb 10 '16 at 14:05

1 Answers1

0

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
Vaseph
  • 704
  • 1
  • 8
  • 20