Xstream错误地将列表项解释为另一个列表项的一部分

时间:2013-11-07 18:27:17

标签: java list xstream

我试图使用Xstream从XML文件中读取。我遇到的一个奇怪的问题是,当我创建一个用户列表时,它会正确读取第一个用户,但它会尝试将第二个用户存储为的成员变量第一个!我有这个XML文件:

<data>
    <users>
        <user>
            <username>test1</username>
            <password>test1</password>
            <firstname>Test</firstname>
            <lastname>One</lastname>
            <email>test1@gmail.com</email>
            <indexedrecords>0</indexedrecords>
        </user>
        <user>
            <username>test2</username>
            <password>test2</password>
            <firstname>Test</firstname>
            <lastname>Two</lastname>
            <email>test2@gmail.com</email>
            <indexedrecords>0</indexedrecords>
        </user>
    </users>
    .... There are other tags here, but they're not causing an issue (yet).
</data>

Xstream阅读器的代码:

XStream xstream = new XStream(new StaxDriver());
xstream.addImplicitCollection(Model.class, "users", User.class);

@SuppressWarnings("rawtypes")
Class[] classes = new Class[2];
classes[0] = Model.class;
classes[1] = User.class;
xstream.processAnnotations(classes);

String xml = "";//The XML is really stored here, and it does work correctly
xstream.fromXML(xml);

班级本身。型号:

@XStreamAlias("data")
public class Model {
    @XStreamImplicit
    public static List<User> users;
    //Other stuff
}

用户:

@XStreamAlias("user")
public class User implements ModelItem{
    private String username;
    private String password;
    @XStreamAlias("firstname")
    private String firstName;
    @XStreamAlias("lastname")
    private String lastName;
    private String email;
    @XStreamAlias("indexedrecords")
    private int recordsIndexed;
}

该例外(原谅[java] - 它正在使用ANT运行):

 [java] Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Element user of type shared.model.User is not defined as field in type shared.model.User
 [java] ---- Debugging information ----
 [java] class               : shared.model.User
 [java] required-type       : shared.model.User
 [java] converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
 [java] path                : /data/users/user
 [java] line number         : 19
 [java] class[1]            : indexer.shared.model.Model
 [java] version             : null
 [java] -------------------------------
 [java]     at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.writeValueToImplicitCollection(AbstractReflectionConverter.java:403)
 [java]     at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:334)
 [java]     at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
 [java]     at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
 [java]     at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:322)
 [java]     at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
 [java]     at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
 [java]     at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
 [java]     at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
 [java]     at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
 [java]     at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
 [java]     at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
 [java]     at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
 [java]     at importer.Importer.importXml(Importer.java:77)
 [java]     at importer.Importer.main(Importer.java:45)

为什么Xstream会尝试将第二个User存储为第一个User内的变量?

回答编辑:有两个问题,迈克尔的回答都解决了这个问题。

1)我暗示了一个隐式集合(两次!),我不应该这样做,因为XML显式声明了users列表。

2)Model类中的变量是静态的。目前还不清楚为什么这会导致错误,但它确实引起了错误。

1 个答案:

答案 0 :(得分:3)

用户列表之前的@XStreamImplicit注释告诉XStream XML没有标签调用用户,而是所有名为user的标签都应存储在用户列表中。

从XML中删除users或从变量声明中删除@XStreamImplicit注释。

其他更改 您还需要移除对xstream.addImplicitCollection(Model.class, "users", User.class);的调用,因为它正在执行与@XStreamImplicit类似的操作。

修复隐式后遇到的另一个问题是由Model类中的users变量声明为static引起的。由于它是静态的,因此不会被视为序列化的一部分。