我正在使用生成此类

XML的系统:

123 Main Street

Suite 123

Test City

AA

00000

两个addressLine元素应该是XStream隐式集合的一部分 – 我想调用getAddressLine()方法并获取List< String>输出.

我一直在使用XStream的教程,并且还没有弄清楚如何正确映射addressLine元素.在XStream’s Tweaking Output tutorial中有类似的用例,但没有提供示例代码:

Another use case are collections, arrays and maps. If a class has a

field that is a one of those types, by default all of its elements are

embedded in an element that represents the container object itself. By

configuring the XStream with the XStream.addImplicitCollection(),

XStream.addImplicitArray(), and XStream.addImplicitMap() methods it is

possible to keep the elements directly as child of the class and the

surrounding tag for the container object is omitted. It is even

possible to declare more than one implicit collection, array or map

for a class, but the elements must then be distinguishable to populate

the different containers correctly at deserialization.

In the following example the Java type representing the farm may have

two containers, one for cats and one for dogs:

06001

但是,此SO answer表明XStream场示例是不可能的.

我已经尝试过这个Java代码对我的Java代码进行单元测试,但还没有运气:

XStream xstream = new XStream(new StaxDriver());

xstream.alias("address", Address.class);

xstream.alias("addressLine", String.class);

xstream.addImplicitCollection(Address.class, "addressLines");

Address address = (Address) xstream.fromXML(

new FileInputStream("src/test/resources/addressTest.xml"));

我应该尝试其他任何配置更改吗?

注意:我目前正在使用XStream v1.2.2.

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐