请注意,这不是我问的另一个问题的重复,“使用 MOXy 和 XPath,是否可以解组属性列表? https://stackoverflow.com/questions/6907225/with-moxy-and-xpath-is-it-possible-to-unmarshal-a-list-of-attributes“很相似,但又不一样。
我的 XML 看起来像这样:
<test>
<items>
<item type="cookie" brand="oreo">cookie</item>
<item type="crackers" brand="ritz">crackers</item>
</items>
</test>
这与我之前问题中的 xml 类似,只不过现在每个项目有两个属性而不是一个。
在我的课堂上:
@XmlPath("items/item/@type")
@XmlAttribute
private ArrayList<String> itemList = new ArrayList<String>();
@XmlPath("items/item/@brand")
@XmlAttribute
private ArrayList<String> brandList = new ArrayList<String>();
感谢我之前问题的答案,我能够解组type
属性到列表中。brandList
,然而,是空的。如果我注释掉注释itemList
(所以它没有被 JAXB/MOXy 填充)然后brandList
包含正确的值。
看来我只能使用 XPath 将单个属性解组到列表中。这是设计使然还是我配置有问题?
Update:看来我也无法从元素中解组文本和属性。如果我的类映射如下:
@XmlPath("items/item/text()")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
@XmlPath("items/item/@brand")
@XmlAttribute
private ArrayList<String> brandList = new ArrayList<String>();
brandList
在这种情况下也是空的。如果我交换顺序和地图brandList
首先然后itemList
是空的。就好像第一个映射消耗了该元素,因此无法读取基于该元素或其属性的进一步值。