Java Unmarshal lista obiektów z JAXB
Mam kod XML, który wygląda następująco:
<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>
Mam klasę ObjectList, która wygląda następująco:
<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>
I klasa obiektów, która wygląda tak:
<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>
Kiedy próbuję odmówić XML do i obiektu za pomocą tego kodu:
<code>JAXBContext jaxbContext = JAXBContext.newInstance(ObjectList.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); RESTResponse response = getObjects(); ObjectList objects = (ObjectList) unmarshaller.unmarshal(new StringReader(response.getResponseBody())); </code>
Otrzymuję następujący błąd:
javax.xml.bind.UnmarshalException: nieoczekiwany element (uri: „”, local: „ObjectList”). Oczekiwane elementy to <{} Object>, <{} objectList>
EDYTOWAĆ: Właśnie zauważyłem kilka problemów Zmieniłem znacznik XmlRootElement mojego obiektu ObjectList na @XmlRootElement (name = "ObjectList") i znacznik XmlRootElement mojego obiektu na @XmlRootElement (nazwa = "obiekt). Teraz otrzymuję i opróżniam listę obiektów.
Każda pomoc jest bardzo ceniona.