title: How to set keys and values of a hashmap as tags and values of a xml file
body: <p>I followed <a href="http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html" rel="nofollow">http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html</a> and wrote my code as follows</p>
  
  <p>Request and response XML</p>
  
  <pre><code>&lt;AlphabetReq&gt;
      &lt;a&gt;Apple&lt;/a&gt;
      &lt;b&gt;Ball&lt;/b&gt;
      &lt;c&gt;Cat&lt;/c&gt; 
      &lt;d&gt;Dog&lt;/d&gt;
      &lt;e&gt;Elephant&lt;/e&gt;
      &lt;f&gt;Fox&lt;/f&gt;
  &lt;/AlphabetReq&gt;
  
  &lt;AlphabetResp&gt;
      &lt;a&gt;Apple&lt;/a&gt;
      &lt;b&gt;Ball&lt;/b&gt;
      &lt;c&gt;Cat&lt;/c&gt; 
      &lt;d&gt;Dog&lt;/d&gt;
      &lt;e&gt;Elephant&lt;/e&gt;
      &lt;f&gt;Fox&lt;/f&gt;
  &lt;/AlphabetResp&gt;
  </code></pre>
  
  <p>AlphabetReq and AlphabetResp classes</p>
  
  <pre><code>import java.util.LinkedHashMap;
  import java.util.Map.Entry;
  import java.util.Set;
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  import org.eclipse.persistence.oxm.annotations.XmlPath;
  
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Alph {
  
  @XmlPath(".")
  @XmlJavaTypeAdapter(AlphAdapter.class)
  private LinkedHashMap&lt;String, String&gt; cLinkedHashMap = new 
          LinkedHashMap&lt;String, String&gt;();
  
  @XmlPath(".")
  @XmlJavaTypeAdapter(AlphAdapter.class)
  private LinkedHashMap&lt;String, String&gt; gLinkedHashMap = new 
          LinkedHashMap&lt;String, String&gt;();
  
  protected void put(String theKey, String theValue) {
      cLinkedHashMap.put(theKey, theValue);   
      gLinkedHashMap.put(theKey, theValue);
  }
  
  protected String get(String theKey) {
      return (String) cLinkedHashMap.get(theKey);
  }
  
  protected Set&lt;Entry&lt;String,String&gt;&gt; getCEntrySet() {
      return cLinkedHashMap.entrySet();
  }
  
  protected Set&lt;Entry&lt;String,String&gt;&gt; getGEntrySet() {
      return gLinkedHashMap.entrySet();
  }
  
  protected LinkedHashMap&lt;String, String&gt; getCLinkedHashMap() {
      return cLinkedHashMap;
  }
  
  protected LinkedHashMap&lt;String, String&gt; getGLinkedHashMap() {
      return gLinkedHashMap;
  }
  
  public String toCXML() throws XMLHandlingException {
      return null;
  }
  
  public String toGXML() throws XMLHandlingException {
      return null;
  }
  
  }
  
  @XmlRootElement(name="AlphReq")
  @XmlDiscriminatorValue("AlphabetReq")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class AlphabetReq extends Alph {
  
  public static AlphabetReq getInstance(String theAlphabetReqXML) throws 
  XMLHandlingException {
      return XMLUtils.parseAlphabetReqXML(theAlphabetReqXML);
  }
  
  public String toCXML() throws XMLHandlingException {
      return XMLUtils.getAlphabetReqXML(this);
  }
  
  }
  
  @XmlRootElement(name="AlphResp")
  @XmlDiscriminatorValue("AlphabetResp")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class AlphabetResp extends Alph {
  
  public static AlphabetResp getInstance(String theAlphabetRespXML) throws 
  XMLHandlingException {
      return XMLUtils.parseAlphabetRespXML(theAlphabetRespXML);
  }
  
  public String toCXML() throws XMLHandlingException {
      return XMLUtils.getAlphabetRespXML(this);
  }
  
  }
  </code></pre>
  
  <p>I created the following XMLUtil Methods for marshalling and unmarshalling</p>
  
  <pre><code>public static String getAlphabetReqXML(Alph theAlphabet) throws XMLHandlingException {
  
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  Writer writer = null;
  
  try {
      writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
  } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
  }
  
  try {
      writer.write("&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?&gt;" );
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  try {
      JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class); 
      Marshaller JMarshaller = JContext.createMarshaller();
      JMarshaller.marshal(theAlphabet, writer);
  } catch (Throwable e) {
      e.printStackTrace();
  }
  
  String theAlphabetReqXML = byteArrayOutputStream.toString();
  return theAlphabetReqXML;
  
  }
  
  public static AlphabetReq parseAlphabetReqXML(String theAlphabetReqXML) throws 
  XMLHandlingException {
  
  if(null == theAlphabetReqXML) {
      return null;
  }
  try {
      InputStream IPStream = new ByteArrayInputStream(theAlphabetReqXML.getBytes());
      JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class);
      Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
      AlphabetReq alphabetreq = (AlphabetReq) JUnmarshaller.unmarshal(IPStream);
      return alphabetreq;
  } catch(Throwable t) {
      t.printStackTrace();
  }
  }
  
  public static String getAlphabetRespXML(Alph theAlphabet) throws XMLHandlingException {
  
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  Writer writer = null;
  
  try {
      writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
  } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
  }
  
  try {
      writer.write("&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?&gt;" );
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  try {
      JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class); 
      Marshaller JMarshaller = JContext.createMarshaller();
      JMarshaller.marshal(theAlphabet, writer);
  } catch (Throwable e) {
      e.printStackTrace();
  }
  
  String theAlphabetRespXML = byteArrayOutputStream.toString();
  return theAlphabetRespXML;
  
  }
  
  public static AlphabetResp parseAlphabetReqXML(String theAlphabetRespXML) throws 
  XMLHandlingException {
  
  if(null == theAlphabetRespXML) {
      return null;
  }
  try {
      InputStream IPStream = new ByteArrayInputStream(theAlphabetRespXML.getBytes());
      JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class);
      Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
      AlphabetResp alphabetresp = (AlphabetResp) JUnmarshaller.unmarshal(IPStream);
      return alphabetresp;
  } catch(Throwable t) {
      t.printStackTrace();
  }
  }
  </code></pre>
  
  <p>and introduced an Adapter class</p>
  
  <pre><code>import java.util.*;
  import java.util.Map.Entry;
  import javax.xml.bind.annotation.*;
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  import org.eclipse.persistence.oxm.annotations.XmlVariableNode;
  
  public class AlphAdapter extends XmlAdapter&lt;AlphAdapter.AdaptedMap, LinkedHashMap&lt;String, String&gt;&gt;{
  
  public static class AdaptedMap {
      @XmlVariableNode("key")
      List&lt;AdaptedEntry&gt; entries = new ArrayList&lt;AdaptedEntry&gt;();  
  }
  
  public static class AdaptedEntry {  
      @XmlTransient
      public String key;
  
      @XmlValue
      public String value;
  }
  
  @Override
  public AdaptedMap marshal(LinkedHashMap&lt;String, String&gt; map) throws Exception {
      AdaptedMap adaptedMap = new AdaptedMap();
      for(Entry&lt;String, String&gt; entry : map.entrySet()) {
          AdaptedEntry adaptedEntry = new AdaptedEntry();
          adaptedEntry.key = entry.getKey();
          adaptedEntry.value = entry.getValue();
          adaptedMap.entries.add(adaptedEntry);
      }
      return adaptedMap;
  }
  
  @Override
  public LinkedHashMap&lt;String, String&gt; unmarshal(AdaptedMap adaptedMap) throws Exception {
      List&lt;AdaptedEntry&gt; adaptedEntries = adaptedMap.entries;
      LinkedHashMap&lt;String, String&gt; map = new LinkedHashMap&lt;String, String&gt;(adaptedEntries.size());
      for(AdaptedEntry adaptedEntry : adaptedMap.entries) {
          map.put(adaptedEntry.key, adaptedEntry.value);
      }
      return map;
  }
  }
  </code></pre>
  
  <p>When I run this I am not getting any values into my adaptedmap. it has keys but values as null</p>
  
  <p>for ex: </p>
  
  <ul>
  <li>key: A, value: null </li>
  <li>key: B, value: null  </li>
  <li>key: C, value: null  </li>
  <li>key: D, value: null  </li>
  <li>key: E, value: null  </li>
  <li>key: F, value: null</li>
  </ul>
  
  <p>Any help is appreciated.</p>
  
  <p>Thanks</p>