title: How to convert XML to java.util.Map and vice versa?
body: <p>I'm searching a lightweight API (preferable single class) to convert a</p>
  <pre><code>Map&lt;String,String&gt; map = new HashMap&lt;String,String();
  </code></pre>
  <p>to XML and, vice versa, convert the XML back to a <code>Map&lt;String,String&gt;</code>.</p>
  <p>example:</p>
  <pre><code>Map&lt;String,String&gt; map = new HashMap&lt;String,String();
  map.put(&quot;name&quot;,&quot;chris&quot;);
  map.put(&quot;island&quot;,&quot;faranga&quot;);
  
  MagicAPI.toXML(map,&quot;root&quot;);
  </code></pre>
  <p>result:</p>
  <pre><code>&lt;root&gt;
    &lt;name&gt;chris&lt;/chris&gt;
    &lt;island&gt;faranga&lt;/island&gt;
  &lt;/root&gt;
  </code></pre>
  <p>and back:</p>
  <pre><code>Map&lt;String,String&gt; map = MagicAPI.fromXML(&quot;...&quot;);
  </code></pre>
  <p>I don't want to use <a href="http://jaxb.java.net/" rel="nofollow noreferrer">JAXB</a> or <a href="http://json.org/java/" rel="nofollow noreferrer">JSON conversion API</a>. It doesn't have to take care of nested maps or attributes or anything else, just that simple case.
  Any suggestions?</p>
  <hr />
  <p>I created a working copy &amp; paste sample. Thanks to <a href="https://stackoverflow.com/users/112079/fvu">fvu</a> and <a href="https://stackoverflow.com/users/70786/michal-bernhard">Michal Bernhard</a>.</p>
  <p><a href="http://xstream.codehaus.org/download.html" rel="nofollow noreferrer">Download latest XStream framework</a>, 'core only' is enough.</p>
  <pre><code>Map&lt;String,Object&gt; map = new HashMap&lt;String,Object&gt;();
  map.put(&quot;name&quot;,&quot;chris&quot;);
  map.put(&quot;island&quot;,&quot;faranga&quot;);
  
  // convert to XML
  XStream xStream = new XStream(new DomDriver());
  xStream.alias(&quot;map&quot;, java.util.Map.class);
  String xml = xStream.toXML(map);
  
  // from XML, convert back to map
  Map&lt;String,Object&gt; map2 = (Map&lt;String,Object&gt;) xStream.fromXML(xml);
  </code></pre>
  <p>No converters or anything else is required. Just the <a href="http://repository.codehaus.org/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar" rel="nofollow noreferrer">xstream-x.y.z.jar</a> is enough.</p>