处理< property key ='name'value ='Foo'/>而不是< name> Foo</name>与JAXB [英] Handling <property key='name' value='Foo' /> instead of <name>Foo</name> with JAXB

查看:59
本文介绍了处理< property key ='name'value ='Foo'/>而不是< name> Foo</name>与JAXB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的XML:

I have XML that looks like this:

 <thing>
    <property key='name' value='Foo' />
 </thing>

我想用JAXB阅读.

我知道我能做

@XmlRootElement(name="thing")
public class Thing{

   @XmlElement(name="name")
   public String name;
}

如果XML看起来像

<thing>
   <name>Foo</name>
</thing>

,但是我该如何处理上面的XML布局?

, but what do I do for the XML layout above?

推荐答案

注意:我是 JSR-222 )专家组.

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2.X (JSR-222) expert group.

在这种情况下,您可以使用MOXy的@XmlPath扩展名:

You can use MOXy's @XmlPath extension for this use case:

@XmlRootElement(name="thing")
public class Thing{

   @XmlPath("property[@key='name']/@value")
   public String name;
}

有关更多信息:

  • http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html
  • http://bdoughan.blogspot.com/2011/05/specifying-eclipselink-moxy-as-your.html

这篇关于处理&lt; property key ='name'value ='Foo'/&gt;而不是&lt; name&gt; Foo&lt;/name&gt;与JAXB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆