I need to deserialize an XML that has dynamic and sequential elements.

I'm using XStream 1.4.

Is there any way to convert to a Generic list using Annotations? Or do I need to extend Converter?

  • XML

Example 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>

Example 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>
  • Classes
@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   
}
  • Output
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

0 Answers