1

I have an android app that I am trying to get a file path for a SAX parser. I have the following structure:

assets:(Where my xml file is)
src(same level as assets)
   com
       msi
          androidrss(The calling java file is in here)

I tried several variations of this:

InputSource is = new InputSource("file://../../../../assets/Rss.xml");

But I always get a FNF Exception

Any suggestions?

3 Answers 3

1

You can't use File APIs to get at data in the assets directory when you're running from your APK. You have to use the Android - AssetManager class. You get an instance of the AssetManager class from the Context object. See Context object

Sign up to request clarification or add additional context in comments.

Comments

1

To elaborate on the answer by @typo.pl:

InputStream in = getContext().getAssets().open("Rss.xml");
// test that in is not null
try {
    // set up your SAX parser and handler
    parser.parse(in, handler);
} finally {
    try { in.close(); }
    catch (IOException ignored) {}
}

Comments

1

Place the xml at /res/raw and open it with:

getResources().openRawResource(R.raw.xmlName);

1 Comment

I think that should be xmlResourceId instead of xmlName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.