How to bind a xml element into an object member variable?

泄露秘密 提交于 2019-12-01 06:44:35

You need to use the @XmlValue annotation on the addressline property.

@XmlAccessorType(XmlAccessType.FIELD)
class Address {
    @XmlValue
    String addressline;
}
will

this is an answer for a similar (but not exactly the same) question that was linked here:

The solution for our issue relates to this question as well. For the issue above, the short answer (as noted there) is to use @XmlValue attribute with the getMessageText(), not @XmlElement. I had already use 'XmlValue', and it still didn't work, so I reverted to XmlElement.

XmlValue wasn't the whole solution in that case. We also found that we need:

  • @XmlAccessorType( XmlAccessType.NONE )

Apparently because of other stuff in the class. Evidentially JABX tries to match every get/set property with XML and apparently it got confused and can't or won't process my XmlValue if there are non-XML POJO properties (I deduce).

  @XmlAccessorType( XmlAccessType.NONE )
  @XmlRootElement(name = "announcement")
  public class Announcement
  {
      ... 

      @XmlValue
      public  String getMessageText(){
          return this.messageText;
      }
  }

Lesson learned. If your JAXB doesn't do what you think it ought to do, I have confused it. Thanks for the help, knowing what is needed to do, let us find what else we needed too.

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