Simple XML deserialization

前端 未结 3 1322
心在旅途
心在旅途 2021-01-13 08:59

I am trying out the Simple XML serializer. I am more interested in deserialization from XML->Java. Here is my code as a unit test:

import java.io.StringReade         


        
3条回答
  •  生来不讨喜
    2021-01-13 09:19

    I don't think you need all that repetition and the extra annotations' attribute. If the name is the same as the object attribute, it will be used by default.

    so you can just declare it as:

    @Root
    class Address {
        @Attribute
        private final String street;
    
        @Attribute
        private final String city;
    
        @Attribute
        private final String state;
    
        public Address(String street, String city, String state) {
            super();
            this.street = street;
            this.city = city;
           this.state = state;
       }
    
       @Override
       public String toString() {
          return "Address [city=" + city + ", state=" + state + ", street=" + street + "]";
       }   
    }
    

提交回复
热议问题