Is it possible to use XStream with an abstract node?
Posted
by Dan Watling
on Stack Overflow
See other posts from Stack Overflow
or by Dan Watling
Published on 2010-04-23T12:36:59Z
Indexed on
2010/04/23
12:43 UTC
Read the original article
Hit count: 390
xstream
|abstract-class
My client application is making calls to a service that returns a common "root" XML, but a different result node. The "root" XML contains possible error codes. Is it possible to use XStream in this scenario? Example:
public class RootNode {
ErrorInfo errorInfo;
BaseResult result;
...
}
public class ErrorInfo {
String message;
...
}
public abstract BaseResult { }
public class SearchResult extends BaseResult {
List<Object> searchResults;
...
}
public class AccountResult extends BaseResult {
String name;
...
}
The XML coming back could be one of two formats:
<root>
<errorInfo><message>...</message></errorInfo>
<result>
<searchResults>...</searchResults>
</result>
</root>
OR
<root>
<errorInfo><message>...</message></errorInfo>
<result>
<name>...</name>
</result>
</root>
I have set up my XStream object as follows:
XStream x = new XStream();
x.alias("root", Root.class);
x.alias("errorInfo", ErrorInfo.class);
x.alias("result", <SearchResult.class OR AccountResult.class depending on what I am expecting back>);
Of course, when I run this I receive an error telling me XStream cannot instantiate the base class (BaseResult). For fun, I also converted the BaseResult into an interface but received a similar error. I've looked through XStream's documentation and it isn't clear to me how to handle a situation like the one I just described. Is it even possible to do using XStream?
Thanks,
-Dan
© Stack Overflow or respective owner