简体   繁体   中英

How can I encourage JAXB to read XmlElement content string?

I'm not getting my "announcements" when I try to parse messages like these:

 <?xml version="1.0" encoding="UTF-8"?>
 <message messageType="SUBSCRIBER" messageName="ANNOUNCEMENT">
     <announcement time="12:30">
         Lunchtime!
     </announcement>
     <announcement time="32:00">
         Good night ...
     </announcement>
     <errorText>Phone number missing, subscriber: Dick</errorText>
 </message>

The JAXB Java class seemed pretty straightforward. And is based on similar messages that work. In this case the difference seems to be that I can have two different types of nested element (XmlElement) in the main block.

The code below actually parses the XML but it doesn't call the Announcement. setMessageText () method. Whereas I can see that the time attribute is set from the XML and I have an array size=2 for announcements, it reads and sets the single errorText XmlElement. Btw , I also removed the tag from the code and XML -- Not change in treatment of my messageText. Ideas welcome!

 @XmlRootElement(name = "message")
 public class AnnouncementMessage 
 { 
      @XmlAttribute
      public String getMessageName(){
        return this.messageName;
      }
      public void   setMessageName( String name ){
        this.messageName = name;
      }

      @XmlAttribute
      public String getMessageType(){
          return messageType;
      }
      public void  setMessageType( String newMessageType ){
          this.messageType = newMessageType;
     }

      @XmlElement(name = "errorText")
      public String getErrorText(){
          return errorText;
      }
      public void setErrorText( String newMsg ){
          this.errorText = newMsg;
      }

     private     List<Announcement>  announcements   = new ArrayList<>();

  @XmlElement(name = "announcement")
  public List<Announcement> getAnnouncements(){
        return this.announcements;
  }
  public void setAnnouncements( List<Announcement> newAnnouncements ){
        this.announcements = newAnnouncements;
     }
 }

And the Announcement class:

 @XmlRootElement(name = "announcement")
 public class Announcement 
 {

     private     String  messageText = "";
     private     String  time        = "12:00";

     XmlAttribute(name ="time")
     public  String  getTime(){
         return this.time;
     }
     public  void    setTime( String newMsg ){
         this.messageText = time;
     }

     @XmlElement(name="announcement")
     public  String  getMessageText(){
         return this.messageText;
     }
     public  void    setMessageText( String newMsg ){
         this.messageText = newMsg;
     }

     Announcement(){
     }
 }

Using the name parameter in the for the XmlElement seem to make no difference. My thanks in advance .

Use the @XmlValue annotation for the message text:

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

Additionally, as noted in this related answer by OP, an @XmlAccessorType(XmlAccessType.NONE) is needed on the Announcement class if it contains getter/setter pairs that are not mapped to/from XML and don't have an @XmlTransient annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM