Load file from resource

2020-03-05 02:23发布

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

Now, I have my XML in a static string inside my unit test class, but it is not very convenient for large 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
2条回答
迷人小祖宗
2楼-- · 2020-03-05 02:49

SAXBuilder has a constructor to read data from file: Document build(java.io.File file) This builds a document from the supplied filename. http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html

查看更多
祖国的老花朵
3楼-- · 2020-03-05 02:55

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")

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).

查看更多
登录 后发表回答