首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xstream出错

Xstream出错
EN

Stack Overflow用户
提问于 2018-08-04 11:11:58
回答 1查看 1.5K关注 0票数 0

我遇到了以下错误:

代码语言:javascript
复制
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 (主代码):

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
import java.util.*;

public class GameList {

    private ArrayList<Game> Games = new ArrayList<>();

    public void setGames(ArrayList<Game> Games2){

        Games.clear();
        Games2 = Games;
    }

}

Game.java代码:

代码语言:javascript
复制
public class Game {

    private int id;
    private String name;
    private String plataforma;

}

我已经设法让Xstream读取不是列表类型的简单xml代码,但是,我现在需要让它读取带有列表的代码。

EN

回答 1

Stack Overflow用户

发布于 2018-08-06 13:12:55

别名是在您尝试读取XML之后设置的,因此XStream无法理解如何处理包含列表元素的XML。

应在读取XML之前设置列表的别名。请将代码修改如下。

代码语言:javascript
复制
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));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51682105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档