I have XML that looks like the following:
<?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>
I have an ObjectList class that looks like the following:
@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;
}
}
And an object class that looks like this:
@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;
}
}
When I try to unmarshal the xml into and object using this code:
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectList.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
RESTResponse response = getObjects();
ObjectList objects = (ObjectList) unmarshaller.unmarshal(new StringReader(response.getResponseBody()));
I get the following error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ObjectList"). Expected elements are <{}Object>,<{}objectList>
EDIT: 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.
Any help is much appreciated.
maybe you should try to change name of your custom class from
Object
to some other? Or ensure that correct instance ofObject
is used inObjectList
classWell, it says expected element:
Object
orobjectList
(starting with a lowercase "o") but it reads anObjectList
(starting with a uppercase "O")!