我有以下分配&使用输入XML收取bean作为 -
public class Allocation {
private Integer allocQty;
private Double allocPrice;
private List<Charge> charges;
}
public class Charge {
private String chargeType;
private String chargeSubType;
}
<allocation>
<allocQty />
<allocPrice />
<charges>
<charge>
<chargeType>A</chargeType>
<chargeSubType>B</chargeSubType>
</charge>
<charge>
<chargeType>C</chargeType>
<chargeSubType>D</chargeSubType>
</charge>
</charges>
</allocation>
尝试使用下面的xstream配置给出了两个Charge的元素列表,但那些Charge对象的所有字段(String)都为NULL !!我在这里错过了什么吗?
XStream xstream = new XStream(new StaxDriver());
xstream.ignoreUnknownElements();
xstream.alias("allocation", Allocation.class);
...
xstream.alias("charge", Charge.class);
xstream.addImplicitCollection(Allocation.class, "charges", "charges", Charge.class);
...
答案 0 :(得分:1)
通过&#34;隐式收集&#34; XStream意味着元素的集合而不包含(&#34; root&#34;)元素(参见configuration manual)。在您的XML charge
元素中, 包含在charges
元素中。因此charges
不是隐式集合,addImplicitCollection
配置错误。因此,您可以按原样保留XML并删除&#39; addImplicitCollection&#39;调用或删除charges
元素,如下所示:
<allocation>
<allocQty />
<allocPrice />
<charge>
<chargeType>A</chargeType>
<chargeSubType>B</chargeSubType>
</charge>
<charge>
<chargeType>C</chargeType>
<chargeSubType>D</chargeSubType>
</charge>
</allocation>
(在后一种情况下,您还必须修正拼写错误:addImplicitCollection
的第三个参数必须是"charge"
,而不是"charges"
。)