首页 > 解决方案 > 使用 XStream 反序列化顺序和动态元素

问题描述

我需要反序列化具有动态和顺序元素的 XML。

我正在使用 XStream 1.4。

有没有办法使用注释转换为通用列表?还是我需要扩展转换器?

示例 1:

<entities>
    <entity>
        <f0>RECIFE</f0>
        <f1>13/01/2017 13:58:47</f1>
    </entity>
    <entity>
        <f0>TERESINA</f0>
        <f1>13/01/2018 13:58:47</f1>
    </entity>
</entities>

示例 2:

<entities>
    <entity>
        <f0>RECIFE</f0>
        <f1>13/01/2017 13:58:47</f1>
        <f2>36ºC</f2>
    </entity>
    <entity>
        <f0>TERESINA</f0>
        <f1>13/01/2018 13:58:47</f1>
        <f2>42ºC</f2>
    </entity>
</entities>
@XStreamAlias("entities")
public class EntitiesResponse {

    @XStreamImplicit(itemFieldName = "entity")
    private List<EntityResponse> entity;

    // getters and setters
}
@XStreamAlias("entity")
@XStreamConverter(value = SerializableConverter.class, strings = {"value"})
public class EntityResponse {

    private Object value;

    // getters and setters   
}
Security framework of XStream not initialized, XStream is probably vulnerable.
Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.mycompany.mavenproject1.response.EntityResponse.f0
---- Debugging information ----
message             : No such field com.mycompany.mavenproject1.response.EntityResponse.f0
field               : f0
class               : com.mycompany.mavenproject1.response.EntityResponse
required-type       : com.mycompany.mavenproject1.response.EntityResponse
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /entities/entity/f0
class[1]            : com.mycompany.mavenproject1.response.EntitiesResponse
required-type[1]    : com.mycompany.mavenproject1.response.EntitiesResponse
version             : 1.4.11.1

标签: javaxstream

解决方案


推荐阅读