Lista Java Unmarshal de objetos com JAXB
Eu tenho XML que se parece com o seguinte:
<code><?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ObjectList> <object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/> <object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/> <object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/> <object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/> <object attributeOne="somedate" attributeTwo="false" attributeThree="id" attributeFour="true"/> </ObjectList> </code>
Eu tenho uma classe ObjectList que se parece com o seguinte:
<code>@XmlRootElement public class ObjectList { @XmlElementWrapper(name = "ObjectList") @XmlElement(name = "Object") private ArrayList<Object> ObjectList; public ArrayList<Object> getObjectList() { return ObjectList; } public void setObjectList(ArrayList<Object> objectList) { ObjectList = objectList; } } </code>
E uma classe de objeto que se parece com isso:
<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>
Quando tento descompactar o xml e objeto usando este código:
<code>JAXBContext jaxbContext = JAXBContext.newInstance(ObjectList.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); RESTResponse response = getObjects(); ObjectList objects = (ObjectList) unmarshaller.unmarshal(new StringReader(response.getResponseBody())); </code>
Estou tendo o erro a seguir:
javax.xml.bind.UnmarshalException: elemento inesperado (uri: "", local: "ObjectList"). Os elementos esperados são <{} Object>, <{} objectList>
EDITAR: Eu só notei alguns problemas que eu mudei a tag XmlRootElement do meu objeto ObjectList para @XmlRootElement (name = "ObjectList") e a marca XmlRootElement do meu objeto para @XmlRootElement (name = "object). Eu não consigo mais a exceção, no entanto Eu recebo e lista vazia de objetos agora.
Qualquer ajuda é muito apreciada.