简体   繁体   English

xstream有条件地解组到一个类

[英]xstream conditionally unmarshall to a class

Because of legacy (unfortunate) reasons, we have the same xml roots for 2 different representations. 由于遗留(不幸的)原因,我们为2个不同的表示形式使用了相同的xml根。 With xstream, how would we let unmarshaller to use a class we need while unmarshalling. 使用xstream,我们如何让解组器在解组时使用所需的类。

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. 我正在考虑传递一些上下文(通过ThreadContext),以便xstream在解组期间将使用该信息来选择正确的类,尽管我不确定从哪里开始。 Any suggestions are very appreciated. 任何建议都非常感谢。

Notes: 笔记:

  • Root tags are same for both XML 两种XML的根标签都相同
  • No other information (attribute) on root tag is available to distingish 2 representations 根标签上没有其他信息(属性)可用于区分2个表示形式
  • Cannot change the xml because of legacy reasons 由于遗留原因,无法更改xml

Ideally I would like the solution to work with Spring-OXM but will take shortcuts if needed 理想情况下,我希望该解决方案可与Spring-OXM一起使用,但在需要时会采取捷径

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. 因此,您可以在开头创建两个xStream实例,并分别为它们配置转换器和别名,并在每个表示形式中使用一个实例。

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. 在我看来,这种方法比设置全局上下文变量然后在转换器中包含一堆if并处理潜在的歧义更干净,更可控。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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