读取位于同一目录中的XML文件 [英] Read a XML file that's in the same directory

查看:119
本文介绍了读取位于同一目录中的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以,我的文件层次结构是:

  + Project 
+ build.xml
+ save.xml
+ src
+ build

我有保存文件来保存给定时刻的游戏状态。它应该很容易覆盖,并易于阅读,再次加载在游戏中的一切。

我保存()是这样的,它似乎工作:

p>

  public void save(Game game)throws IOException {
Document doc = DocumentHelper.createDocument();
doc.add(game.save());
文件保存=空;
save = new File(./ save.xml);

FileWriter writer = new FileWriter(save);
doc.write(作家);
writer.close();



$ b

game.save()是游戏中的一种方法,想做。它是递归地和所有的,我知道它工作正常,因为我打开了另一个程序的XML文件。

所以,我的问题从这里开始。我的getinfofromxml()方法是:

  public Game getinfofromxml()throws IOException {
Game game;
SAXReader reader = new SAXReader();
尝试{
URL fileWithData = getClass()。getResource(./ save.xml);
Document document = reader.read(fileWithData);
Element alreadySavedGame = document.getRootElement();
game = getGameSaved(alreadySavedGame);
} catch(DocumentException ex){
throw new IOException();
}
返回游戏;



$ b $ p
$ b

我的问题是,当试图运行它(通过ant)时,没有测试将传递。我去日食,我可以看到,当我尝试加载游戏,它会抛出在线程AWT-EventQueue-0java.lang.NullPointerException异常。 / p>

我试过改变 URL fileWithData = getClass()。getResource(./ save.xml); URL fileWithData = getClass()。getResource( ./save.xml); 和类似的东西,但它总是相同的。



有什么想法吗?



感谢您阅读

解决方案

如果使用getResource使用用于加载该类的类加载器来查找您的文件。这可能是(至少晚些时候)你的应用程序打包在jar文件,我不认为这就是你想从那里加载你的存储游戏:)



  URL fileWithData = new File(save.xml) 。.toURI()的toURL(); 

(您可能需要额外的例外)

I know it's been asked before and answered, but i just can't make it work.

So, my files hierarchy is:

+ Project
  + build.xml
  + save.xml
  + src
  + build

I have the "save" file to save the state of a game in a given instant. It should be easy to overwrite and easy to read to load everything again in the game.

My save() is like this, and it seems to be working:

 public void save(Game game) throws IOException{
    Document doc = DocumentHelper.createDocument();
    doc.add(game.save());
    File save=null;
    save = new File("./save.xml");

    FileWriter writer = new FileWriter(save);
    doc.write( writer);
    writer.close();
}

game.save() is a method in the game that actually does what i want to do. It does it recursively and all, and i know it works fine because i opened the XML file with another program.

So, my problems begins here. My getinfofromxml() method is:

public Game getinfofromxml() throws IOException{
    Game game;
    SAXReader reader = new SAXReader();
    try{
        URL fileWithData=getClass().getResource("./save.xml");
        Document document = reader.read(fileWithData);
        Element alreadySavedGame= document.getRootElement();
        game= getGameSaved(alreadySavedGame);
    }catch(DocumentException ex){
        throw new IOException();
    }
    return game;
} 

My problem is, when try to run it (via ant), no test will pass. I go to eclipse, and i can see that when i try to load the game, it throws Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.

I've tried changing the line URL fileWithData=getClass().getResource("./save.xml"); to URL fileWithData=getClass().getResource("save.xml"); and URL fileWithData=getClass().getResource("../save.xml"); and things like that, but it says always the same.

Any idea?

Thank you for reading

解决方案

so, if you use getResource() it will try to find your file using the classloader that was used to load that class. this might be (later at least) the jar file your app is packaged in, and i don't think that's you're trying to load your savegame from there :)

i'd load the file the same way you save it:

URL fileWithData= new File( "save.xml" ).toURI().toURL(); 

(you might have to catch an extra exception)

这篇关于读取位于同一目录中的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆