I am trying to read from an XML file using Xstream. The weird issue I am having is that when I make a list of users, it reads the first user correctly, but it tries to store the second user as a member variable of the first! I have this XML file:
<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>
The code for the Xstream reader:
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);
And the classes themselves. Model:
@XStreamAlias("data")
public class Model {
@XStreamImplicit
public static List<User> users;
//Other stuff
}
User:
@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;
}
The exeption (pardon the [java]
- it's being run using 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)
Why is Xstream trying to store the second User
as a variable inside the first User
?
Answer Edit: There were two problems, both addressed in Michael's answer.
1) I was implying an implicit collection (twice!) which I should not have done, since the XML explicitly declares a users
list.
2) My variables in the Model
class was static. It is unclear as to why this caused an error, but it did cause it nevertheless.
The @XStreamImplicit
annotation before the user list tells XStream that the XML will not have a tag call users but instead all tags called user should be stored in the users list.
Either remove users
from the XML or remove @XStreamImplicit
annotation from the variable declaration.
Additional changes
You also need to remove the call to xstream.addImplicitCollection(Model.class, "users", User.class);
since that is doing something similar to the @XStreamImplicit
.
The other problem your encountering after fixing the implicit is caused by the users variable in your Model class being declared static. Since it is static it is not being treated as part of the serialization.