XStream 找不到要解组的特定 class XML

XStream cannot find the specific class to unmarshall XML

我想从两个 classes.I 中读取包含存储为 ArrayList 的信息的两个 xml 文件的内容必须使用 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.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)

xstreamin 中,您还没有为 Account 添加别名 class。

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