我的 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 类:
@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;
}
}
对象类如下所示:
@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 解组到对象中时:
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:“”,本地:“ObjectList”)。预期元素为 、
EDIT:我刚刚注意到几个问题,我将 ObjectList 对象的 XmlRootElement 标记更改为 @XmlRootElement(name = "ObjectList") 并将对象的 XmlRootElement 标记更改为 @XmlRootElement(name = "object)。但是,我不再遇到异常我现在得到了一个空的对象列表。
任何帮助深表感谢。
好吧,它说的是预期的元素:Object
or objectList
(以小写“o”开头)但它读取ObjectList
(以大写“O”开头)!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)