How do I map the inner text content of an element to a Class property?

我与影子孤独终老i 提交于 2019-12-10 10:44:11

问题


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
}

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?


回答1:


You can use the @XmlValue annotation.

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

For More Information

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


来源:https://stackoverflow.com/questions/13026825/how-do-i-map-the-inner-text-content-of-an-element-to-a-class-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!