title: JAXB Unmarshalling XML Class Cast Exception
body: <p>I'm using this <a href="https://gist.github.com/itavero/5858958" rel="nofollow">JAXB Collection Generics</a> to unmarshall my string xml and return the List type. 
  Here's the methods I used.</p>
  
  <pre><code>public static &lt;T&gt; List&lt;T&gt; unmarshalCollection(Class&lt;T&gt; cl, String s)
          throws JAXBException {
      return unmarshalCollection(cl, new StringReader(s));
  }
  
  public static &lt;T&gt; List&lt;T&gt; unmarshalCollection(Class&lt;T&gt; cl, Reader r)
          throws JAXBException {
      return unmarshalCollection(cl, new StreamSource(r));
  }
  
  
  private static &lt;T&gt; List&lt;T&gt; unmarshalCollection(Class&lt;T&gt; cl, Source s)
          throws JAXBException {
      JAXBContext ctx = JAXBContext.newInstance(JAXBCollection.class, cl);
      Unmarshaller u = ctx.createUnmarshaller();
      JAXBCollection&lt;T&gt; collection = u.unmarshal(s, JAXBCollection.class).getValue();
      return collection.getItems();
  }
  </code></pre>
  
  <p>Example getters and setters:</p>
  
  <pre><code>   @XmlRootElement(name = "person")
  class Person{
  
      private String firstName;
      private String lastName;
      private String address;
  
  
      public String getFirstName() {
          return firstName;
      }
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
      public String getLastName() {
          return lastName;
      }
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
      public String getAddress() {
          return address;
      }
      public void setAddress(String address) {
          this.address = address;
      }
  
      @Override
      public String toString() {
          return "Person [firstName ="+firstName+" , lastName = "+lastName+" , address = "+address+"]"; 
      }
  
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>public static void main(String[] args) throws JAXBException {
          String xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;person&gt;&lt;firstName&gt;Foo&lt;/firstName&gt;&lt;lastName&gt;Bar&lt;/lastName&gt;&lt;address&gt;U.S&lt;/address&gt;&lt;/person&gt;";
          List&lt;Person&gt; p = unmarshalCollection(Person.class,xml);
          for(Person person : p ){
              System.out.println(person);
          }
      }
  </code></pre>
  
  <p>Exception thrown</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to  com.Person
  at com.JAXBUtil.main(JAXBUtil.java:62)
  </code></pre>
  
  <p>What did I do wrong? Any ideas?Thanks.</p>