我有一个格式为的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'总是只有一个子元素,并且该子元素可以被调用并包含任何内容。
我尝试使用:
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;
}
}
但它一直在抛出非法的例外情况!
答案 0 :(得分:0)
如果您注释该字段(实例变量),则需要添加以下类型级别注释:
@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;
}
}
答案 1 :(得分:0)
为每个错误添加@XmlAnyElement(lax = true)(javax.xml.bind.JAXBElement没有默认构造函数)