如何将元素的内部文本内容映射到Class属性? [英] How do I map the inner text content of an element to a Class property?

查看:113
本文介绍了如何将元素的内部文本内容映射到Class属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我分别拥有以下XML和Java代码:

Say I have the following XML and Java code respectively:

<foo>
My text content
</foo>



@XmlRootElement( name="foo" )
public static class Foo
{
  // This is where I want to see "My text content" stored
  private String text;

  // getters and setters
}

当我试过编组XML,我的 Foo 实例没有使用我的 foo 的内部文本中的值填充其text属性给定XML中的元素。我该如何解决这个问题?

When I tried marshalling the XML, my Foo instance doesn't get its text property populated with value from the inner text of my foo element in the given XML. How do I solve this?

推荐答案

你可以使用 @XmlValue 注释。

@XmlValue 
public String getText() {
    return text;
}

更多信息

  • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html

这篇关于如何将元素的内部文本内容映射到Class属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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