Java Unmarshal list of objects with JAXB

2019-02-19 22:11发布

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.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-19 22:50

maybe you should try to change name of your custom class from Object to some other? Or ensure that correct instance of Object is used in ObjectList class

查看更多
叛逆
3楼-- · 2019-02-19 22:56

Well, it says expected element: Object or objectList (starting with a lowercase "o") but it reads an ObjectList (starting with a uppercase "O")!

查看更多
登录 后发表回答