title: JAXB unmarshal XML elements to HashMap
body: <p>I found a lot of articles that describe how to unmarshal a sequence of XML elements to a HashMap as long as they are within a "parent" element. However, I do not get this to work with the children directly under the root element!</p>
  
  <p>Option 1 - Works:</p>
  
  
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;checks&gt;
    &lt;checks&gt;
      &lt;check key="check1"/&gt;
      &lt;check key="check2"/&gt;
      ...       
    &lt;/checks&gt;
  &lt;/checks&gt;
  </code></pre>
  
  <p>Option 2 - Does <em>not</em> work:</p>
  
  
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;checks&gt;
    &lt;check key="check1"/&gt;
    &lt;check key="check2"/&gt;
    ...
  &lt;/checks&gt;
  </code></pre>
  
  <p>Checks:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.foo.conf;
  
  import java.util.Map;
  
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement; 
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  
  @XmlRootElement(name="checks")
  public class Checks {       
      @XmlJavaTypeAdapter(ChecksAdapter.class)
      @XmlElement(name="checks")
      public Map&lt;String, Check&gt; checkMap;     
  }
  </code></pre>
  
  <p>Check:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.foo.conf;
  
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlValue;
  
  public class Check {
      @XmlAttribute public String key;
      @XmlValue public String description;
  
      public Check() { }
  
      public Check(String key) {
          this.key = key;
      }
  
      public String getCheckKey() {
          return this.key;
      }
  }
  </code></pre>
  
  <p>CheckMapType:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.foo.conf;
  
  import java.util.List;
  
  import javax.xml.bind.annotation.XmlElement;
  
  class CheckMapType { 
      @XmlElement(name="check")
      public List&lt;Check&gt; checkList; // = new ArrayList&lt;Check&gt;();
  }
  </code></pre>
  
  <p>ChecksAdapter:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.foo.conf;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  
  final class ChecksAdapter extends XmlAdapter&lt;CheckMapType, Map&lt;String, Check&gt;&gt; {
  
      @Override
      public CheckMapType marshal(Map&lt;String, Check&gt; arg0) throws Exception {
          return null;
      }
  
      @Override
      public Map&lt;String, Check&gt; unmarshal(CheckMapType arg0) throws Exception {
          System.out.println("u: " + arg0.checkList.size());
          Map&lt;String, Check&gt; map = new HashMap&lt;String, Check&gt;();
  
          for (Check check : arg0.checkList) {
              System.out.println(check);
              map.put(check.key, check);
          }
          return map;
      }       
  }
  </code></pre>
  
  <p>This is (some dummy test lineS) how I generate the classes/invoke the unmarshalling:</p>
  
  <pre class="lang-java prettyprint-override"><code>JAXBContext jc = JAXBContext.newInstance(Checks.class);
  Unmarshaller u = jc.createUnmarshaller();
  Checks c = (Checks) u.unmarshal(new File("checks.xml"));
  System.out.println(c.checkMap.size());
  </code></pre>
  
  <p>Any idea on how to get option #2 to work? It works when using a List instead of the Map but I need the HashMap as I have to access the objects by the given keys...</p>
  
  <p>Any hints much appreciated!</p>