As requested, posting my comment as an answer:
What about adding an attribute to the <respmsg>
tag stating which type it is?
For example:
<respmsg type="LocationResponseMessage">
Pergunta
I have this xml format as output from different http request.
<transaction>
<respmsg>...</respmsg>
<respcode>0000</respcode>
<status>success</status>
</transaction>
Below is my Transaction Class Representation
@root
class Transaction {
@Element
public ResponseMessage respmsg;
@Element
public int respcode;
@Element
public String status;
}
An abstract class representation of "respmsg" property
abstract class ResponseMessage {
}
This is fairly easy to map to a Class object. However "respmsg" is different for every request for login request the respmsg might be
<respmsg>
<firstname>John</firstname>
<lastname>Doe</lastname>
<mobilephone>1234</mobilephone>
<email>johndoe@test.com</email>
</respmsg>
class LoginResponseMessage extends ResponseMessage {
@Element
public String firstname;
@Element
public String lastname;
@Element
public String mobilephone;
@Element
public String email;
}
and for location request it might be
<respmsg>
<address1>121 121 UAE Bldg, dubai</address1>
<address2/>
<city>Dubai</city>
<postalcode>121</postalcode>
<region>dubai</region>
</respmsg>
class LocationResponseMessage extends ResponseMessage {
@Element
public String address1;
@Element
public String address2;
@Element
public String postalcode;
@Element
public String region;
}
How do I design my class so that it knows what type of class to use for the respmsg during desserialization?
String xmlData = retrieve(urls[0]); //retrieve fetches the string xml from the http request
Serializer serializer = new Persister();
Reader reader = new StringReader(xmlData);
Transaction t = serializer.read(Transaction.class, reader, false);
Solução
As requested, posting my comment as an answer:
What about adding an attribute to the <respmsg>
tag stating which type it is?
For example:
<respmsg type="LocationResponseMessage">