我有一个看起来像这样的 xml 文档
<root>
<subElement>
<a>an instance</a>
<b>another instance</b>
<problemElement>
<option>1st instance</option>
<option>2nd instance</option>
<option>3rd instance</option>
</problemElement>
</subElement>
<subElement>
<a></a>
<b></b>
<problemElement>
<option>instance</option>
</problemElement>
</subElement>
</root>
我的 jaxb 类看起来像这样;
@XmlRootElement(name = "root")
@XmlType(name = "Root")
public class Root{
@XmlElement(name = "subElement", required = true)
private final List<SubElement> subElementList = new ArrayList<SubElement>();
public List<SubElement> getSubElementList() {
return subElementList;
}
}
@XmlType(name = "SubElement", propOrder = {"a", "b", "problemElement"})
public abstract class SubElement{
@XmlElement(required = true)
private String a;
@XmlElement(required = true)
private String b;
@XmlElement(name = "problemElement", required = true)
private List<ProblemElement> problemElement= new ArrayList<ProblemElement>();
public String getA() {
return a;
}
public String getB() {
return b;
}
public List<ProblemElement> getProblemElement() {
return problemElement;
}
}
@XmlType(name = "ProblemElement" )
public class ProblemElement {
@XmlElement(required = true)
private String option;
public String getOption() {
return option;
}
}
除了problemElement,一切正常。该列表仅返回 xml 中的最后一个选项节点值,在本例中为“3rd instance”。我究竟做错了什么?