我遇到了以下错误:
Security framework of XStream not initialized, XStream is probably vulnerable.
Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field GameList.Game
---- Debugging information ----
message : No such field GameList.Game
field : Game
class : GameList
required-type : GameList
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /GameList/Game
line number : 2
version : not available
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.handleUnknownField(AbstractReflectionConverter.java:524)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:375)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
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:1486)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1466)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1337)
at Start.main(Start.java:11)我试图在GameList.xml文件的GameList中获取游戏对象,但是,它总是返回这个错误。
Start.java (主代码):
import com.thoughtworks.xstream.*;
import java.io.*;
import java.util.*;
public class Start {
public static void main(String[] args)throws FileNotFoundException{
XStream xstream = new XStream();
FileReader reader = new FileReader("Games.xml");
GameList gamelist = new GameList();
gamelist.setGames((ArrayList<Game>) xstream.fromXML(reader));
xstream.alias("Game", Game.class);
xstream.alias("Games", GameList.class);
xstream.addImplicitCollection(GameList.class, "Games", Game.class);
}
}GameList.java代码:
import java.util.*;
public class GameList {
private ArrayList<Game> Games = new ArrayList<>();
public void setGames(ArrayList<Game> Games2){
Games.clear();
Games2 = Games;
}
}Game.java代码:
public class Game {
private int id;
private String name;
private String plataforma;
}我已经设法让Xstream读取不是列表类型的简单xml代码,但是,我现在需要让它读取带有列表的代码。
发布于 2018-08-06 13:12:55
别名是在您尝试读取XML之后设置的,因此XStream无法理解如何处理包含列表元素的XML。
应在读取XML之前设置列表的别名。请将代码修改如下。
public static void main(String[] args)throws FileNotFoundException {
XStream xstream = new XStream();
xstream.alias("Game", Game.class);
xstream.alias("Games", GameList.class);
xstream.addImplicitCollection(GameList.class, "Games", Game.class);
FileReader reader = new FileReader("Games.xml");
GameList gamelist = new GameList();
gamelist.setGames((ArrayList<Game>) xstream.fromXML(reader));
}https://stackoverflow.com/questions/51682105
复制相似问题