使用EclipseLink MOXy JAXB实现,我试图使用@XmlPath批注基于元素的属性值获取元素值。我似乎无法正常工作。支持吗?

XML摘录:
<Item>
...
<ItemRefFields>
<ItemRefField id="1">12345</ItemRefField>
<ItemRefField id="2">blah</ItemRefField>
</ItemRefFields>
</Item>

POJO摘录:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Item")
public class Item
{
...
@XmlPath("ItemRefFields/ItemRefField[@id='1']/text()")
private String ItemRef1 = null;
@XmlPath("ItemRefFields/ItemRefField[@id='2']/text()")
private String ItemRef2 = null;
...
}

现在发生的是,两个值都连续分配给ItemRef2,这样“ blah”最终成为最终值,但ItemRef1从未获得分配的值。我相信这是因为XPath表达式([@ id ='x'])的属性值部分被忽略了。因此,两个XPath表达式都被视为相同,这似乎导致该表达式首先被映射到ItemRef1,然后再映射到ItemRef2,同时ItemRef2覆盖ItemRef1映射,因此ItemRef2获胜。

我希望这是由我自己的语法问题引起的。任何意见,将不胜感激。

谢谢,
凯文

最佳答案

我领导了EclipseLink JAXB (MOXy),这个feature是即将到来的EclipseLink 2.3版本的一部分。您可以从每晚从以下位置下载EclipseLink 2.3.0夜间下载之一(从3月22日开始)来进行尝试:


http://www.eclipse.org/eclipselink/downloads/nightly.php


映射将与您在问题中描述的一样:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Item")
public class Item
{
  ...
  @XmlPath("ItemRefFields/ItemRefField[@id='1']/text()")
  private String ItemRef1 = null;
  @XmlPath("ItemRefFields/ItemRefField[@id='2']/text()")
  private String ItemRef2 = null;
  ...
}


想要查询更多的信息


http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html

10-02 09:45