这是我想要的XML结构
<Outer type="good" id="1">
<Uid>123</Uid>
<Name>Myself</Name>
<Inner type="bad">This Value</Inner>
</Outer>
这是我的对象。
@XmlAccessorType(XMLAccessType.FIELD)
@XmlType(name="Outer", propOrder = {
"uid"
"name"
"inner"
})
public class Outer{
@XmlElement(name = "Uid")
protected String uid;
@XmlElement(name = "Name")
protected String name;
@XmlElement(name = "Inner")
protected Inner inner;
public static class Inner{
@XmlAttribute
private String type;
@XmlValue
private String value;
//setters & getters for both
}
//setters & getters for all the elements
}
现在在我班上我正在做
Outer o = new Outer();
o.setUid/ID/Type/Name() ; //all the setter
Inner i - new Inner();
i.setValue("This Value");
i.setType("bad");
当Irun,我正在
If a class has @XmlElement property, it cannot have @XmlValue property.
并且
Class has two properties of the same name "type" (This one is for the Source class)
并且
Class has two properties of the same name "value" (This one is for Source class too)
发生了什么,我能做些什么来纠正这个问题?
由于
答案 0 :(得分:2)
目前,JAXB威胁两个字段(由于注释)和两个get / set(由于默认访问器类型)作为属性。所以你的班级Inner
有4个属性。
请为Inner
类
@XmlAccessorType(XmlAccessType.FIELD)
public static class Inner
{
或者注释属性而不是字段
public static class Inner
{
private String type;
private String value;
@XmlAttribute
public String getType()
{
return type;
}
// setter setType
@XmlValue
public String getValue()
{
return value;
}
// setter setValue
}
答案 1 :(得分:0)
将XmlRootElement注释添加到Outer类,除此之外它应该可以工作。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Outer", propOrder = {"uid", "name", "inner"})
public static class Outer {