title: mapping key value xml pair to java map using xstream
body: <p>I am trying to convert a xml file as follows into java map.
  <strong>xml</strong></p>
  
  <pre><code>&lt;Person&gt;
     &lt;ID&gt;123&lt;/ID&gt;
     &lt;Demographics&gt;
     &lt;LastName&gt;ABC&lt;/LastName&gt;
     &lt;FirstName&gt;XYZ&lt;/FirstName&gt;
     &lt;/Demographics&gt;
     &lt;Married&gt;YES&lt;/Married&gt;
  &lt;/Person&gt;
  </code></pre>
  
  <p>The XStream code is as follows:</p>
  
  <pre><code>    final XStream xstream = new XStream();
      xstream.alias("Person", Map.class);
      xstream.alias("Demographics", Map.class);
      xstream.registerConverter(new MapEntryConverter());
  
      final Map&lt;String, Object&gt; map2 = (Map&lt;String, Object&gt;) xstream.fromXML(xml);//where xml is above defined string.
  </code></pre>
  
  <p>The custom MapEntryConverter is:</p>
  
  <pre><code>public class MapEntryConverter implements Converter
  {
      public boolean canConvert(final Class clazz)
      {
          return AbstractMap.class.isAssignableFrom(clazz);
      }
  
      public void marshal(final Object value, final HierarchicalStreamWriter writer, final MarshallingContext context)
      {
          final AbstractMap&lt;String, String&gt; map = (AbstractMap&lt;String, String&gt;) value;
          for (final Entry&lt;String, String&gt; entry : map.entrySet())
          {
              writer.startNode(entry.getKey().toString());
              writer.setValue(entry.getValue().toString());
              writer.endNode();
          }
      }
  
      public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context)
      {
          final Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
          while (reader.hasMoreChildren())
          {
              reader.moveDown();
              map.put(reader.getNodeName(), reader.getValue());
              reader.moveUp();
          }
          return map;
      }
  }
  </code></pre>
  
  <p>So the map now only consists of ID and Married as key-value pairs. Its not converting Demographics parent into its corresponding name value pairs.</p>
  
  <p>I would like to have all the tag names as keys and their values as map values.</p>