使用Xstream将具有对象列表的XML解析为javaobject

时间:2014-05-20 16:41:49

标签: java xml xstream

我想使用Xstream将XML转换为java对象。以下是我的实现方式。

我有一个看似如下的XML:

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

我有2个java对象,我想用Xstream转换上面的XML:

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

}

我的Xstream代码是(正文是上面提到的XML):

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

我得到以下异常:

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

1 个答案:

答案 0 :(得分:1)

问题在于Map ATSP#STATM。您尝试反序列化

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

但是使用您的配置,您的xml必须具有以下格式:

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

如果您想原样使用xml,请向其注册NamedMapConverter

    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);

修改 使用注释更容易,而不是以编程方式注册转换器

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