【问题标题】:Error with XstreamXstream 出错
【发布时间】:2018-08-04 03:11:58
【问题描述】:

我遇到以下错误:

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 中获取 Game 对象,但是它总是返回该错误。

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 代码,但是,我现在需要让它读取带有列表的代码。

【问题讨论】:

    标签: xml list arraylist compiler-errors xstream


    【解决方案1】:

    在您尝试读取 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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 2020-03-29
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2015-04-30
      • 1970-01-01
      相关资源
      最近更新 更多