简体   繁体   中英

xstream conditionally unmarshall to a class

Because of legacy (unfortunate) reasons, we have the same xml roots for 2 different representations. With xstream, how would we let unmarshaller to use a class we need while unmarshalling.

I am thinking of passing some context (through ThreadContext) so that xstream would use that information to pick the right class during unmarshalling, though I am not sure where to start. Any suggestions are very appreciated.

Notes:

  • Root tags are same for both XML
  • No other information (attribute) on root tag is available to distingish 2 representations
  • Cannot change the xml because of legacy reasons

Ideally I would like the solution to work with Spring-OXM but will take shortcuts if needed

You know in advance which of the two representations you are about to parse.

So you can create two instances of the xStream in the beginning, and configure the converters and aliases differently for each of them, and use one instance per representation.

This approach seems to me cleaner and more controllable than setting a global context variable and then having a bunch of if s inside the converters, and dealing with potential ambiguities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM