XStream 短动态别名 [英] XStream short dynamic aliases
问题描述
我想要类的简称,现在我可以用别名来实现
I want to have short names for classes, now i can do it with aliases
XStream x = new XStream();
x.alias("dic", Dic.class);
但是我必须为每个类手动定义别名,有没有办法配置 xstream 使其自动执行?
but i have to define alias manually for every class, is there any way to configure xstream to do it automatically?
推荐答案
在内部,XStream 使用其 Mapper
接口来处理类和字段到它们在 XML 中的相应名称的映射.这个接口有大量的实现.XStream
类本身可以在其构造函数中使用 Mapper
.您可能想要查看该类的源代码以查看它默认使用的 Mapper 实现,然后编写您自己的实现来自动为您执行别名.例如,ClassAliasingMapper
看起来很有用.
Internally, XStream uses its Mapper
interface to handle the mapping of classes and fields to their corresponding names in the XML. There are a large number of implementations of this interface. The XStream
class itself can take a Mapper
in its constructor. You might want to check out the source code of that class to see which Mapper implementation it uses by default, and then write your own implementation that automatically does your aliasing for you. ClassAliasingMapper
looks useful, for example.
这篇关于XStream 短动态别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!