从资源加载文件加载、文件、资源

2023-09-06 04:46:14 作者:在你背后

综上所述:为了方便地添加单元测试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).