Load file from resource
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?
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).
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
精彩评论