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.StringReader;
import java.io.StringWriter;
import junit.framework.TestCase;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class SimpleTest extends TestCase {
public void testWriting() throws Exception {
StringWriter writer = new StringWriter();
Address address = new Address("1234 Main Street", "San Francisco", "CA");
Serializer serializer = new Persister();
serializer.write(address, writer);
System.out.println("Wrote: " + writer.getBuffer());
}
public void testReading() throws Exception {
String input = "<address street='1234 Main Street' city='San Francisco' state='CA'/>";
Serializer serializer = new Persister();
System.out.println("Read back: " + serializer.read(Address.class, new StringReader(input)));
}
}
@Root
class Address {
@Attribute(name="street")
private final String street;
@Attribute(name="city")
private final String city;
@Attribute(name="state")
private final String state;
public Address(@Attribute(name="street") String street, @Attribute(name="city") String city, @Attribute(name="state") String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}
@Override
public String toString() {
return "Address [city=" + city + ", state=" + state + ", street=" + street + "]";
}
}
This works, but the repeated @Attribute
annotations (at the field and at the constructor argument) in the Address class look ugly. Is there some way to:
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 + "]";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With