2

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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

You're right, I resolved that problem already (see EDIT) however, I am now getting a null objectlist back
I think it would help if you posted the updated code and (more importantly) the input XML.
I'll mark yours as the answer, because it did end up being a case issue. I got rid of the exception by fixing my case on the custom objects XmlRootElement tags. The empty list was also because I still had the XmlElement tag in the ObjectList with an uppercase O when it needed to be lowercase
0

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

1 Comment

My class isn't actually named ObjectList or Object, I just used this for my example code in this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.