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

后端 未结 2 1606
误落风尘
误落风尘 2021-01-13 19:39

I\'m trying to unmarshal an xml to an object using moxy.Below is the sample of the xml.


    
        value

        
相关标签:
2条回答
  • 2021-01-13 20:35

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

    • How can I encourage JAXB to read XmlElement content string?

    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.

    0 讨论(0)
  • 2021-01-13 20:38

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

    @XmlAccessorType(XmlAccessType.FIELD)
    class Address {
        @XmlValue
        String addressline;
    }
    
    0 讨论(0)
提交回复
热议问题