Search code examples
javaxmlxstream

Parsing XML having list of objects into javaobject using Xstream


I want to convert a XML to java object using Xstream. Below is how my implementation to do this.

I have a XML which looks like:

<CWATSR>
<ATSP>
    <Keys>spacekey1</Keys>
    <Keys>spacekey2</Keys>
    <Keys>spacekey3</Keys>
    <STATM>
        <entry>
            <key>Test1</key>
            <value>Test1value</value>
        </entry>
        <entry>
            <key>Test2</key>
            <value>Test2value</value>
        </entry>
        <entry>
            <key>ITest3</key>
            <value>Test3Value</value>
        </entry>
    </STATM>
</ATSP>
<ATSP>
    <Keys>spacekey11</Keys>
    <Keys>spacekey22</Keys>
    <Keys>spacekey33</Keys>
    <STATM>
        <entry>
            <key>Test1</key>
            <value>Test1value1</value>
        </entry>
        <entry>
            <key>Test2</key>
            <value>Test2value2</value>
        </entry>
        <entry>
            <key>ITest3</key>
            <value>Test3Value3</value>
        </entry>
    </STATM>
</ATSP>
</CWATSR>

and I have 2 java objects for which I want to convert the above XML using Xstream:

CWATST.class

@XStreamAlias("CWATSR")
public class CWATSR {

    @XStreamImplicit
    private List<ATSP> atsp;

    //Getters and setters
}

WrappedReflectingDefinition.class

@XStreamAlias("ATSP")
public class ATSP implements Serializable {

    @XStreamImplicit(itemFieldName = "Keys")
    @XmlElement(name = "Keys")
    private List<String> Keys;

    @XStreamAlias("STATM")
    private Map<String, String> STATM;

    // Getters and setters

}

My Xstream code is (body is the XML stated above):

xstream.processAnnotations(CWATSR .class);
xstream.processAnnotations(ATSP.class);
CWATSR cwatsr = (CWATSR ) xstream.fromXML(body);

I get the following exception:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Keys:Keys
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:464)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:396)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:247)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1156)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1140)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1011)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1002)
    at com.iit.package.delete.main(delete.java:53)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: Keys
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:98)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:91)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:85)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:80)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 16 more

Solution

  • The problem is the Map ATSP#STATM. You try to deserialize

        <entry>
            <key>Test1</key>
            <value>Test1value</value>
        </entry>
    

    But with your configuration your xml must have this format:

        <entry>
            <string>Test1</string>
            <string>Test1value</string>
        </entry>
    

    If you want to use your xml as it is, register a NamedMapConverter to it.

        final String entryName = "entry";
        final String keyName = "key";
        final Class<String> keyType = String.class;
        final String valueName = "value";
        final Class<String> valueType = String.class;
        NamedMapConverter statmConverter = new NamedMapConverter(
                xstream.getMapper(), entryName, keyName, keyType, valueName,
                valueType);
        xstream.registerLocalConverter(ATSP.class, "STATM", statmConverter);
    

    EDIT: It is easier to use annotation instead of registering the converter programatically

        @XStreamAlias("STATM")
        @XStreamConverter(value = NamedMapConverter.class, strings = { "entry",
                "key", "value" }, types = { String.class, String.class })
        Map<String, String> STATM;