Java使用JAXB解组对象列表 [英] Java Unmarshal list of objects with JAXB

查看:136
本文介绍了Java使用JAXB解组对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML如下所示:

<?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>

我有一个类似于以下内容的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;
    }
}

当我尝试将xml解组并使用对象时此代码:

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()));

我收到以下错误:

javax.xml.bind.UnmarshalException:意外元素(uri:,local:ObjectList)。预期的元素是< {} Object>,< {} objectList>

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ObjectList"). Expected elements are <{}Object>,<{}objectList>

编辑:
我刚注意到一些问题我将我的ObjectList对象的XmlRootElement标记更改为@XmlRootElement(name =ObjectList),将我的Object的XmlRootElement标记更改为@XmlRootElement(name =object)。我不再获得异常,但是我得到了空列表对象现在。

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.

非常感谢任何帮助。

推荐答案

,它说预期元素:对象 objectList (以小写o开头)但它读取 ObjectList (以大写O开头)!

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

这篇关于Java使用JAXB解组对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆