使用XStream对一个Set的内容进行别名 [英] Aliasing contents of a Set using XStream
问题描述
我在XStream中有别名混淆的问题。
I have a problem with aliasing in XStream.
我有一组String项目,我想像这样序列化为XML:
I've got a set of String items that I would like to serialize to XML like this:
<types>
<type>abc</type>
<type>def</type>
</types>
但是,我似乎找不到一个很好的方法来解决这个问题。我试过一个字符串列表,但后来我最终以
However, I can't seem to find a good way to solve this. I've tried a list of strings, but then I end up with
<types>
<string>abc</string>
<string>def</string>
</types>
我也试过把String放在一个简单的类中,
I've also tried putting the String in a simple class, but then I get
<types>
<type>
<aType>abc</aType>
</type>
</types>
其中< type>
的自定义类,而aType是类中的属性,即我使用这种方法获得一个级别太多。我如何去消除额外的级别或者用自定义标签名替换< string>
?
where <type>
is the alias of the custom class, and aType is the attribute in the class, i.e I get one level too much using this approach. How would I go about eliminating the extra level or simply substituting the <string>
with a custom tag name?
推荐答案
您需要别名列表
和列表中的项目。
You will need to alias both the List
and the items in the list.
List<String> types = new ArrayList<String>();
types.add("abc");
types.add("def");
XStream xstream = new XStream();
xstream.alias("types", List.class);
xstream.alias("type", String.class);
System.out.println(xstream.toXML(types));
会导致
<types>
<type>abc</type>
<type>def</type>
</types>
这篇关于使用XStream对一个Set的内容进行别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!