title: JAXB Unmarshalling: List of objects
body: <p>I have </p>
  
  <pre><code>@XmlRootElement(namespace = "http://www.w3.org/2005/Atom", name = "content")
  @XmlType(name = "course")
  public class Course implements Resource
  
  ...
  
  @XmlElementWrapper(name="subcourses")
  @XmlElement(name="course")
  List&lt;Xlink&gt; subcourses;         //!?
  </code></pre>
  
  <p>and Xlink class, which works fine in inline variable.</p>
  
  <pre><code>public class Xlink
  {
      private String href;
      private String value;
  
      @XmlAttribute(namespace = "http://www.w3.org/1999/xlink")
      public String getHref()
      {
          return href;
      }
  
      public void setHref(String href)
      {
          this.href = href;
      }
  
      @XmlValue
      public String getValue()
      {
          return value;
      }
  
      public void setValue(String value)
      {
          this.value = value;
      }
  }
  </code></pre>
  
  <p>for an XML input of </p>
  
  <pre><code>&lt;atom:content atom:type="xml" xsi:type="course"&gt;
  ...
     &lt;subcourses&gt;
        &lt;course xlink:href="course1"&gt;Some course&lt;/course&gt;
        &lt;course xlink:href="course2"&gt;other course&lt;/course&gt;
  </code></pre>
  
  <p>And subcourses refuses to be unmarshalled (without any exception being thrown).</p>
  
  <p>Note: MOXy is unfortunately not an option.</p>
  
  <p>Edit: new marshalled object</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns3:content xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://www.w3.org/2005/Atom"&gt;
      &lt;code&gt;SOME CODE&lt;/code&gt;
      &lt;name&gt;name&lt;/name&gt;
      &lt;subcourses&gt;
          &lt;course ns2:href="Some href"&gt;some value&lt;/course&gt;
          &lt;course ns2:href="sdsdg"&gt;sdfhdfhdhdh&lt;/course&gt;
      &lt;/subcourses&gt;
  &lt;/ns3:content&gt;
  </code></pre>
  
  <p>Edit2: after some experimentation with unmarshalling and marshalling a test object I have discovered that I need to define xmlns namespace in the header of the content to match <code>xlink:href=</code> like <code>xmlns:xlink="http://www.w3.org/1999/xlink"</code> the problem is that I am getting the Course element from inside a wrapper class that is parsed out by resteasy. Thus the resulting class does not carry over the namespace information.</p>
  
  <p>I somehow need to force JAXB to understand that <code>xmlns:xlink="http://www.w3.org/1999/xlink"</code> applies to the course elements, but after an hour of google I am at a loss.</p>
  
  <p>Edit3: I am getting my object from </p>
  
  <p><a href="https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Entry.java" rel="nofollow">https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Entry.java</a> </p>
  
  <p>which is used on the server counterpart. Which in turn is a part of </p>
  
  <p><a href="https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Feed.java" rel="nofollow">https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Feed.java</a> </p>
  
  <p>Relevant parts of my unmarshalling code are:</p>
  
  <pre><code>Feed f = r.readEntity(Feed.class);
  out.addAll(unmarshaller.Unmarshal(f.getEntries(), clazz));
  </code></pre>
  
  <p>where <code>r</code> is a <code>javax.ws.rs.core.Response</code>. And the unmarshaller</p>
  
  <pre><code>public List&lt;T&gt; Unmarshal(List&lt;Entry&gt; entries, Class clazz)
  {
      List&lt;T&gt; out = new ArrayList&lt;T&gt;();
      T instance;
      for (Entry e : entries)
      {
          try
          {
              JAXBContext context = JAXBContext.newInstance(clazz);
              Unmarshaller unmarsh = context.createUnmarshaller();
              instance = (T) unmarsh.unmarshal((Node) e.getContent());
  </code></pre>
  
  <p>Since this is my first tangle with this technology, it is entirely possible that this code is 'wtf'.</p>