请注意,这不是我询问的另一个问题的重复,“With MOXy and XPath, is it possible to unmarshal a list of attributes?”它是相似的,但不一样。使用MOXy和XPath,是否可以解组两个属性列表?
我有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将单个属性解组到列表中。这是由设计还是我配置了错误的东西?
更新:看来我不能解组文本和元素的属性。如果我的类映射是这样的:
@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
为空。就好像第一个映射消耗元素,所以基于该元素或其属性的更多值不能被读取。
你有最新版本的Moxy吗?我遇到了一些使用旧版本(在更新到最新版本时消失)的XPath属性过滤错误。 – Thilo
我有几个星期前下载的EclipseLink 2.3。根据他们的下载页面,v2.3似乎是最新的。 – Paul
该配置目前不会给你你正在寻找的输出。我会尽量把你可以使用的替代方案放在一起。注意:我是EclipseLink JAXB(MOXy)的领导者。 –