Reputation: 45
I am working on an android application in which I want to parse an xml file as a local resource. I use jdom to parse it, but I have a probleme, I can not open the file and I don't know why. The error is at this line :
document = builder.build(new File("res/xml/data.xml"));
The file is located into the folder res/xml of my project. I got this error:
java.io.IOException: Couldn't open file:/res/xml/data.xml
Caused by java.io.FileNotFoundException :res/xml/data.xml
I tried this:
document = builder.build(new File("data.xml"));
but it did not work. I don't know why the file is not found.
Would you have an idea?
Thanks in advance
Thank you for your suggestion. The 'Uri path' works but not the line
document = builder.build(new File(path));
The method class File must have a type entree String. I tried this:
document = builder.build(new File(path.getPath());
but it returned null and
document = builder.build(new File(path.toString());
I got the same error. Would you have an idea ? Thanks in advance !!
Upvotes: 3
Views: 3154
Reputation: 1
InputStream is = context.getResources().openRawResource(R.raw.data);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
document = builder.build(br);
Be sure to put the data.xml in the res/raw-folder.
Upvotes: 0
Reputation: 1752
I believe you will need to use an URI in your file constructor that points to the data.xml file.
Try something like this:
Uri path = Uri.parse("android.resource://my.package.here/" + R.xml.data);
document = builder.build(new File(path));
This tutorial here might also be helpful: http://androidbook.blogspot.com/2009/08/referring-to-android-resources-using.html
Upvotes: 2
Reputation: 2074
If this is a resource inside your application then you should use Resources methods suchas getXml(). You can get an instance of the rources class from the Context object
Upvotes: 1