tags:

views:

371

answers:

2

Good morning,

To sumup : 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 ?

Regards, Quentin

+1  A: 

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

Moisei
Thanks, I did not know for this method of SAXBuilderBut my message was not clear, my problem is: I do not know what to do to obtain a File object from my xml file.For instance when I call the fileList() method of my current activity context, I get an empty array.Maybe I have to add some information in my Eclipse project in order to have access to this file from the application.
Quentin
given your file is named input.xml and it is located in the current folder of your application, you can call:Document doc = (new SAXBuilder()).build("input.xml");
Moisei
+1  A: 

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

Erich Douglass
Thank you. I'll try it next monday when I'll be at the office.
Quentin