开发者

EclipseLink MOXy @XmlPath support for getting element value by attribute value

Using EclipseLink MOXy JAXB implementation, I'm trying to use @XmlPath annotation to get element values based on the value of an attribute of the element. I can't seem to get it to work. Is this supported?

XML Excerpt:

<Item>

  ...

  <ItemRefFields>

    <ItemRefField id="1">12345</ItemRefField>

    <ItemRefField id="2">blah</ItemRefField>

  </ItemRefFields>

</Item>

POJO Excerpt:

@XmlAccessorType(XmlAcces开发者_运维技巧sType.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;

  ...

}

What happens now is that both values are assigned to ItemRef2 in succession such that "blah" ends up being the final value, but ItemRef1 never gets a value assigned. I believe this is because the attribute value part of the XPath expression ([@id='x']) is being ignored. So both XPath expressions are treated as the same and it appears that this is causing the expression to get mapped first to ItemRef1, then to ItemRef2, with ItemRef2 overwriting ItemRef1 mapping, so ItemRef2 wins.

I'm hoping this is being caused by a syntactical issue on my part. Any advice would be appreciated.

Thanks, Kevin


I lead EclipseLink JAXB (MOXy), and this feature is part of the upcoming EclipseLink 2.3 release. You can try it today by downloading one of the EclipseLink 2.3.0 nightly downloads (starting March 22) from:

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

The mapping will be just as you described in your question:

@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;
  ...
}

For more information

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜