Simple XML deserialization

前端 未结 3 1323
心在旅途
心在旅途 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:17

    Java will serialize all of your class members by default if they implement Serializable and adhere to the JavaBean syntax.

    0 讨论(0)
  • 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 + "]";
       }   
    }
    
    0 讨论(0)
  • 2021-01-13 09:24

    I had similar concern but with pretty complicated objects structures. I solved it by using JAXB library for marshaling and de-marshaling which is a pretty common one. You can consider also totally separating the marshaling logic from your java class by using aspects - you can treat all the annotations in a separate aspect what will make your java class fully clean from marshaling annotations.

    0 讨论(0)
提交回复
热议问题