Java Unmarshal lista de objetos con JAXB
Tengo XML que se parece a lo siguiente:
<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>
Tengo una clase ObjectList que se parece a lo siguiente:
<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>
Y una clase de objeto que se ve así:
<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>
Cuando trato de descomprimir el xml en un 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>
Obtuve el siguiente error:
javax.xml.bind.UnmarshalException: elemento inesperado (uri: "", local: "ObjectList"). Los elementos esperados son <{} Object>, <{} objectList>
EDITAR: Acabo de notar un par de problemas. Cambié la etiqueta XmlRootElement de mi objeto ObjectList a @XmlRootElement (name = "ObjectList") y la etiqueta XmlRootElement de mi objeto a @XmlRootElement (name = "object). Sin embargo, ya no tengo la excepción. Recibo y vacía la lista de objetos ahora.
Cualquier ayuda es muy apreciada.