[英]Extract value from list of xml elements with jaxb
我有一个XML文档,结构像
<thing>
<attr name="one">first</attr>
<attr name="two">second</attr>
<attr name="three">third</attr>
</thing>
我有这样设置的JAXB类:
public class Thing {
List<Attribute> attr = new ArrayList<Attribute>();
@XmlElement(name="attr")
public List<Attribute> getAttr() { return this.attr; }
public void setAttr(List<Attribute> attr) { this.attr = attr; }
}
public class Attribute {
String value;
String name;
@XmlAttribute
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
@XmlElement
public String getValue() { return this.value;}
public void setValue(String value) { this.value = value; }
}
当我解组文档时,如果我循环for (Attribute a : thing.getAttr())
进行a.getName()
它将打印“一个”,“两个”,“三个”,但是a.getValue()
只是空值。
我注释不正确吗? 即使将注释设置为@XmlElement(name="attr")
它似乎也可以执行相同的操作。
这将解决您的问题:
@XmlValue
public String getValue() { return this.value;}
这不是子元素,而是您要查找的当前元素值。
编辑:和顺便说一句,您应该在Thing类上添加@XmlRootElement
批注,以防丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.