3

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 !!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user1086225
  • 45
  • 1
  • 4

3 Answers3

2

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

Lasse Samson
  • 1,752
  • 3
  • 18
  • 17
1

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

nwaltham
  • 2,067
  • 1
  • 22
  • 40
0
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.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
fleck
  • 1