title: Java Unmarshal list of objects with JAXB
body: <p>I have XML that looks like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ObjectList&gt;
    &lt;object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/&gt;
    &lt;object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/&gt;
    &lt;object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/&gt;
    &lt;object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/&gt;
    &lt;object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/&gt;
  &lt;/ObjectList&gt;
  </code></pre>
  
  <p>I have an ObjectList class that looks like the following:</p>
  
  <pre><code>@XmlRootElement
  public class ObjectList {
  
      @XmlElementWrapper(name = "ObjectList")
      @XmlElement(name = "Object")
      private ArrayList&lt;Object&gt; ObjectList;
  
      public ArrayList&lt;Object&gt; getObjectList() {
          return ObjectList;
      }
  
      public void setObjectList(ArrayList&lt;Object&gt; objectList) {
          ObjectList = objectList;
      }
  }
  </code></pre>
  
  <p>And an object class that looks like this: </p>
  
  <pre><code>@XmlRootElement(name = "Object")
  public class Object {
  
      Date attributeOne;
      boolean attritbuteTwo;
      String attributeThree;
      boolean attributeFour;
  
      @XmlAttribute
      public Date getAttributeOne() {
          return attributeOne;
      }
      public void setAttributeOne(Date attributeOne) {
          this.attributeOne = attributeOne;
      }
  
      @XmlAttribute
      public boolean isAttributeTwo() {
          return attritbuteTwo;
      }
      public void setAttributeTwo(boolean attritbuteTwo) {
          this.AttributeTwo = AttributeTwo;
      }
  
      @XmlAttribute
      public String getAttributeThree() {
          return attributeThree;
      }
      public void setAttributeThree(String attributeThree) {
          this.attributeThree = attributeThree;
      }
  
      @XmlAttribute
      public boolean isAttributeFour() {
          return attributeFour;
      }
      public void setAttributeFour(boolean attributeFour) {
          this.attributeFour = attributeFour;
      }
  }
  </code></pre>
  
  <p>When I try to unmarshal the xml into and object using this code:</p>
  
  <pre><code>JAXBContext jaxbContext = JAXBContext.newInstance(ObjectList.class);
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  
  RESTResponse response = getObjects();
  
  ObjectList objects = (ObjectList) unmarshaller.unmarshal(new StringReader(response.getResponseBody()));
  </code></pre>
  
  <p>I get the following error:</p>
  
  <p>javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ObjectList"). Expected elements are &lt;{}Object>,&lt;{}objectList></p>
  
  <p><strong>EDIT:</strong> 
  I just noticed a couple problems I changed the XmlRootElement tag of my ObjectList object to @XmlRootElement(name = "ObjectList") and the XmlRootElement tag of my Object to @XmlRootElement(name = "object). I no longer get the exception, however I get and empty list of objects now. </p>
  
  <p>Any help is much appreciated.</p>