简体   繁体   English

XStream-元帅/非元帅的别名?

[英]XStream - different alias for marshal/unmarshal?

Some old XML still has the canonical class name. 一些旧的XML仍具有规范的类名。 The new XML has the simple name. 新的XML具有简单的名称。

I want to be able to read both formats but make sure that when I marshal I get the simple names. 我希望能够读取两种格式,但要确保在编组时能得到简单的名称。 I have tried using... 我尝试使用...

xstream.alias("package.name.className", Clazz.class);
xstream.alias("className", Clazz.class);

the unmarshalling works fine but the marshalling is mixed. 编组工作正常,但编组混杂。 For some classes I get the simple name, others the canonical. 对于某些类,我得到简单的名称,而对于其他类,则得到规范的名称。

Any idea how to achieve this with the same xstream instance? 知道如何使用相同的xstream实例实现这一点吗?

The beautiful way would be writing a custom Converter for your class. 最好的方法是为您的课程编写自定义的Converter。

The other way is have one xstream instance for unmarshalling with both alias and another xstream instance for marshalling only with the alias you want. 另一种方法是让一个xstream实例用于同时使用别名进行编组,而另一个xstream实例仅用于使用所需别名进行编组。

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

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