How can I recommend JAXB to read the XmlElement content line?

I don’t get my “ads” when I try to parse such messages:

 <?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 Java JAXB class seemed pretty simple. And based on similar messages that work. In this case, the difference is that I can have two different types of nested elements (XmlElement) in the main block.

The code below actually parses the XML, but it does not call the declaration method. setMessageText (). While I see that the time attribute is set from XML, and I have an array size = 2 for declarations, it reads and sets a single XTextElement file. By the way, I also removed the tag from the code and XML - did not change the processing of my Text message. Ideas are 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 ad 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(){
     }
 }

name XmlElement, -, . .

+1
1

@XmlValue :

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

, OP, Announcement @XmlAccessorType(XmlAccessType.NONE), getter/setter, / XML @XmlTransient.

+2

Source: https://habr.com/ru/post/1543351/


All Articles