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
Java will serialize all of your class members by default if they implement Serializable
and adhere to the JavaBean syntax.
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 + "]";
}
}
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.