title: JAXB unmarshal elements of XML to Map
body: <p>I have an XML file. In this file, some of the elements are have attributes that change. I want to put those attributes into a Map. How do I do this?</p>
  
  <p>My XML is:</p>
  
  <pre><code>&lt;ROW id='1'&gt;
      &lt;MOBILE&gt;9831138683&lt;/MOBILE&gt;
      &lt;VARS&gt;
          &lt;CAUSE&gt;Delayed payment&lt;/CAUSE&gt;
          &lt;DO&gt;100.56&lt;/DO&gt;
          &lt;LOT&gt;1&lt;/LOT&gt;
      &lt;/VARS&gt;
  &lt;/ROW&gt;
  &lt;ROW id='2'&gt;
      &lt;MOBILE&gt;9831138684&lt;/MOBILE&gt;
      &lt;VARS&gt;
          &lt;NAME&gt;hi&lt;/NAME&gt;
          &lt;ADDRESS&gt;Here&lt;/ADDRESS&gt;
          &lt;LOT&gt;2&lt;/LOT&gt;
      &lt;/VARS&gt;
  &lt;/ROW&gt;
  </code></pre>
  
  <p>In this, the <code>VARS</code> element can have attributes which changes and I do not know beforehand what these elements will be.</p>
  
  <p>I have created a class for this purpose:</p>
  
  <pre><code>@XmlRootElement(name = "ROW")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class SMSDetail {
      @XmlAttribute
      private int id;
      @XmlElement(name = "MOBILE")
      private int mobileNo;
      @XmlElement(name = "VARS")
      @XmlJavaTypeAdapter(MapAdapter.class)
      private HashMap&lt;String, String&gt; variableMap;
  
      public int getId() {
          return id;
      }
  
      public int getMobileNo() {
          return mobileNo;
      }
  
      public HashMap&lt;String, String&gt; getVariableMap() {
          return variableMap;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public void setMobileNo(int mobileNo) {
          this.mobileNo = mobileNo;
      }
  
      public void setVariableMap(HashMap&lt;String, String&gt; variableMap) {
          this.variableMap = variableMap;
      }
  }
  </code></pre>
  
  <p>I want to map the <code>VARS</code> element to a <code>Map</code>. I want the tags such as <code>CAUSE</code>, <code>LOT</code> to be keys and their values to be the values in the map. I have written an <code>XmlAdapater</code> for this purpose:</p>
  
  <pre><code>public class MapAdapter extends XmlAdapter&lt;MapElements[], Map&lt;String, String&gt;&gt; {
      public MapAdapter() {
      }
  
      @Override
      public MapElements[] marshal(Map&lt;String, String&gt; arg0) throws Exception {
          MapElements[] mapElements = new MapElements[arg0.size()];
          int i = 0;
          for (Map.Entry&lt;String, String&gt; entry : arg0.entrySet())
              mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
  
          return mapElements;
      }
  
      @Override
      public Map&lt;String, String&gt; unmarshal(MapElements[] arg0) throws Exception {
          Map&lt;String, String&gt; r = new TreeMap&lt;String, String&gt;();
          for (MapElements mapelement : arg0)
              r.put(mapelement.key, mapelement.value);
          return r;
      }
  }
  
  class MapElements {
      @XmlAttribute
      public String key;
      @XmlAttribute
      public String value;
  
      private MapElements() {
      } //Required by JAXB
  
      public MapElements(String key, String value) {
          this.key = key;
          this.value = value;
      }
  }
  </code></pre>
  
  <p>This adapter is giving me <code>null</code> for the <code>variableMap</code> variable. How should the Adapter be modified for this?</p>