我想从两个类中读取包含存储为xml
的信息的两个ArrayList
文件的内容。我必须使用XStream
并且写入文件没有问题,但从他们那里读书似乎会引起问题。这两个类是:
public class CreditCard
{
private String creditCard;
private ArrayList<String> tokens;
public CreditCard(String creditCard, ArrayList<String> tokens)
{
this.creditCard = creditCard;
this.tokens = tokens;
}
}
和
public class Account
{
private String username;
private String password;
private AccessRights accessRights;
public Account(String username, String password, AccessRights accessRights)
{
this.username = username;
this.password = password;
this.accessRights = accessRights;
}
}
AccessRights
只是`
public enum AccessRights {
FULL_RIGHTS,
TOKENS_ONLY,
CREDIT_CARD_ONLY;
}
我写入main函数中的文件进行测试。
public static void main(String[] args) throws IOException, ClassNotFoundException
{
XStream xstream = new XStream(new StaxDriver());
Writer someWriter = new FileWriter("Accounts.xml");
ObjectOutputStream out = xstream.createObjectOutputStream(someWriter);
Account one = new Account("Pesho", "0000", AccessRights.FULL_RIGHTS);
Account two = new Account("Kosio", "0809", AccessRights.TOKENS_ONLY);
ArrayList<Account> accounts = new ArrayList<>();
accounts.add(one);
accounts.add(two);
xstream.alias("account", Account.class);
out.writeObject(accounts);
out.close();
XStream xstreamcr = new XStream(new StaxDriver());
Writer someWritercr = new FileWriter("CreditCards.xml");
ObjectOutputStream outcr = xstreamcr.createObjectOutputStream(someWritercr);
ArrayList<String> tokens = new ArrayList<>();
tokens.add("1234243434269991");
tokens.add("7892641434269991");
CreditCard crone = new CreditCard("4563960122019991", tokens);
ArrayList<CreditCard> test = new ArrayList<>();
xstreamcr.alias("creditcard", CreditCard.class);
test.add(crone);
outcr.writeObject(test);
outcr.close();
}
我想要一个从这些文件中读取的void函数。这样的事情:
public void read(){
ArrayList<Account> accountsin;
XStream xstreamin = new XStream(new StaxDriver());
Reader someReader = new FileReader("Accounts.xml");
ObjectInputStream in = xstreamin.createObjectInputStream(someReader);
accountsin = (ArrayList<Account>) in.readObject();
in.close();
}
当然这不起作用,我不知道为什么。我读了一些关于隐式集合的内容,但这对我没什么帮助。因此,编写此函数的任何帮助都非常有帮助:)
我得到的错误是:
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: account : account
---- Debugging information ----
message : account
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : account
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /list/account
line number : 1
version : 1.4.7
-------------------------------
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.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:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1157)
at com.thoughtworks.xstream.XStream$4.readFromStream(XStream.java:1851)
at com.thoughtworks.xstream.core.util.CustomObjectInputStream.readObjectOverride(CustomObjectInputStream.java:120)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:365)
at Server.TokenizationWithRMI.main(TokenizationWithRMI.java:67)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: account
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.SecurityMapper.realClass(SecurityMapper.java:71)
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:47)
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)
答案 0 :(得分:1)
在xstreamin
中,您尚未添加Account
类的别名。
XStream xstreamin = new XStream(new StaxDriver());
//you need to add this line
xstreamin.alias("account", Account.class);
//then the rest of your code...
Reader someReader = new FileReader("Accounts.xml");
ObjectInputStream in = xstreamin.createObjectInputStream(someReader);
accountsin = (ArrayList<Account>) in.readObject();
in.close();