[英]Unmarshalling generic child using Jaxb
我有一个xml文件,格式为:-
<item>
<item_attribute index="1" type="1" >
<name>value1</name>
</item_attribute>
<item_attribute index="2" type="1" >
<a_differnt_name>value2</a_different_name>
</item_attribute>
<item_attribute index="5" type="2" >
<another_name>value3</another_name>
</item_attribute>
</item>
我正在使用JAXB解组xml,并为除“ item_attribute”的孩子之外的每个元素设置了一个类。 我想一般地在每个“ item_attribute”元素中解组数据(元素名称和元素值),而不知道该元素被称为什么。
我所知道的是,“ item_attribute”始终只有1个子元素,该子元素可以被调用并包含任何内容。
我尝试使用:
public class Item_attribute {
private int index;
private Object data;
@XmlAttribute(name = "index")
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@XmlAnyElement(lax = true)
public Object getData() {
return this.data;
}
}
但它总是抛出一个非法注释异常!
如果注释字段(实例变量),则需要添加以下类型级别注释:
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlAnyElement(lax = true)
private Object data;
public Object getData() {
return this.data;
}
}
或者,您可以将注释放在get方法上。
public class Foo {
private Object data;
@XmlAnyElement(lax=true)
public Object getData() {
return this.data;
}
}
将@XmlAnyElement(lax = true)添加到每个错误中(javax.xml.bind.JAXBElement没有默认的构造函数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.