[Simple-support] Ordering of Elements when Serializing Subclass Hierarchy.
Brought to you by:
niallg
|
From: Stan C. <sta...@mo...> - 2007-11-13 17:30:04
|
Is there a way to ensure the ordering of the serialized XML elements
when using subclasses?
Simplified example.
@Root(name = "response")
public class ClassA {
@Element(name = "code", required = true)
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getXmlResponse() throws Exception {
Serializer serializer = new Persister(new Format(4, "<?xml
version=\"1.0\" encoding=\"UTF-8\"?>"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.write(this, out);
out.flush();
return out.toString();
}
}
@Root(name = "response")
public class ClassB extends ClassA {
@Element(name = "status", required = false)
private String status;
@Element(name = "transactionCode", required = false)
private String transactionCode;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTransactionCode() {
return transactionCode;
}
public void setTransactionCode(String transactionCode) {
this.transactionCode = transactionCode;
}
}
In the Simple example above I'm trying to get this:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<code>200</code>
<status>Completed</status>
<transactionCode>101</transactionCode>
</response>
but I get this:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>Completed</status>
<transactionCode>101</transactionCode>
<code>200</code>
</response>
Thanks,
Stan
|