Java Unmarshal Liste der Objekte mit JAXB
Ich habe XML, das wie folgt aussieht:
<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>
Ich habe eine ObjectList-Klasse, die wie folgt aussieht:
<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>
Und eine Objektklasse, die so aussieht:
<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>
Wenn ich versuche, die XML zu dekomprimieren und ein Objekt mit diesem Code zu erstellen:
<code>JAXBContext jaxbContext = JAXBContext.newInstance(ObjectList.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); RESTResponse response = getObjects(); ObjectList objects = (ObjectList) unmarshaller.unmarshal(new StringReader(response.getResponseBody())); </code>
Ich erhalte folgenden Fehler:
javax.xml.bind.UnmarshalException: unerwartetes Element (uri: "", local: "ObjectList"). Erwartete Elemente sind <{} Object>, <{} objectList>
BEARBEITEN: Ich habe gerade ein paar Probleme festgestellt, bei denen ich das XmlRootElement-Tag meines ObjectList-Objekts in @XmlRootElement (name = "ObjectList") und das XmlRootElement-Tag meines Object in @XmlRootElement (name = "object) geändert habe. Die Ausnahme wird jedoch nicht mehr angezeigt Ich bekomme jetzt eine leere Liste von Objekten.
Jede Hilfe wird sehr geschätzt.