EclipseLink MOXy @XmlPath 支持通过属性值获取元素值
使用 EclipseLink MOXy JAXB 实现,我尝试使用 @XmlPath 注释来根据元素属性的值获取元素值。我似乎无法让它发挥作用。支持吗?
XML 摘录:<项目>
...
POJO 摘录:@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="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 获胜。
我希望这是由我的语法问题引起的。任何建议将不胜感激。
谢谢, 凯文
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(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;
...
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我领导 EclipseLink JAXB (MOXy),而这个 功能 是即将发布的 EclipseLink 2.3 版本的一部分。您今天可以通过以下位置下载 EclipseLink 2.3.0 每晚下载之一(从 3 月 22 日开始)进行尝试:
映射将正如您在问题中所描述的那样:
有关更多信息
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:
The mapping will be just as you described in your question:
For more information