title: JAXB unmarshalling tree structure
body: <p>I have XML input which is essentially a tree structure. The goal is to (un)marshall the code into Java classes.</p>
  
  <pre><code>&lt;config&gt;
  
  &lt;key-value-pair&gt;
     &lt;key&gt;Key1&lt;/key&gt;
     &lt;value&gt;Value1&lt;/value&gt;
  &lt;/key-value-pair&gt;
  
  &lt;key-value-pair&gt;
     &lt;key&gt;Key2&lt;/key&gt;
     &lt;value&gt;
        &lt;key-value-pair&gt;
           &lt;key&gt;Subkey2&lt;/key&gt;
           &lt;value&gt;Value999&lt;/value&gt;
        &lt;/key-value-pair&gt;
     &lt;/value&gt;
  &lt;/key-value-pair&gt;
  
  &lt;/config&gt;
  </code></pre>
  
  <p>The XML contains typical <strong>Key/Value pairs</strong>. And <strong>each Value can contain either another Key/Value pair, or List Key/Value pairs or just a single String value</strong>.</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  static class KeyValuePair {
  
      @XmlElement(name="key")
      private String key;
  
      @XmlElement(name="value")
      private String value;   // here I don't know how to reflect
                              // the choice of String or another
                              // list of KeyValuePair objects
      @XmlElement(name="value")
      private List&lt;KeyValuePair&gt; valuePairs;
  
      // getters/setters here
  }
  </code></pre>
  
  <p>Then I have just another wrapper Class</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlRootElement(name="config")
  @XmlAccessorType(XmlAccessType.FIELD)
  static class Structure {
  
      @XmlElement(name="key-value-pair")
      private List&lt;KeyValuePair&gt; keyValuePair;
  
      // getters/setters
   }
  </code></pre>
  
  <p>And here is the logic I am trying to use for (un)marshaling.</p>
  
  <pre class="lang-java prettyprint-override"><code> Unmarshaller jaxbUnmarshaller = jc.createUnmarshaller();
   StringReader reader = new StringReader(input);
   Structure struct = (Structure) jaxbUnmarshaller.unmarshal(reader);
   // struct doesn't get the data correctly ...
  </code></pre>
  
  <p>This is what I got so far. It doesn't work the way I have it, but I am hoping I got myself clear what the end goal should look like.</p>
  
  <p><strong>I want to take the XML at the beginning and put in the instance of Structure class.</strong></p>