从资源加载文件 [英] Load file from resource

查看:93
本文介绍了从资源加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

综上所述:为了方便地添加单元测试SAX解析器,我想从文件加载XML

To sum up: in order to add easily unit tests for a SAX parser I would like to load XML from a file.

现在,我有我的在我的单元测试类的静态XML字符串,但它不是大型的XML非常方便。

Now, I have my XML in a static string inside my unit test class, but it is not very convenient for large XML.

这就是为什么我想一些XML文件添加到我的项目,并加载它们在我的单元测试。我怎样才能做到这一点?

This is why I would like to add some XML files to my project and load them in my unit test. How can I do this?

推荐答案

这个问题被标记为机器人,我注意到你提到的活动,所以我要去假设你想在一个Android应用程序加载XML文件。如果是这样的话,把你的XML文件在 /资产,并呼吁:

This question is tagged as "Android" and I noticed that you mentioned an Activity, so I'm going to assume that you're trying to load an XML file within an Android application. If that is the case, put your XML file under /assets and call:

InputStream is = getAssets().open("input.xml")

活动。从那里,你可以操纵它到 SAXBuilder 。 (或者,如果你只是想以一个单元测试外调试),如果你设置你的测试,以在模拟器上运行这只会工作。

from your Activity. From there, you can manipulate it into SAXBuilder. This will only work if you've set up your test to run on the emulator (or if you're just trying to debug outside of a unit test).

这篇关于从资源加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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