Axiomatic.it
Axiomatic.it

Reputation: 63

Reading Asset Xml File

I want to read an xml file placed in assets/ directory. I've tried this solution (finded online)

InputStream istr = context.getAssets().open("stanza.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
factory.setNamespaceAware(true); 
XmlPullParser xrp = factory.newPullParser(); 
xrp.setInput(istr, "UTF-8");

but return me FileNotFoundException, but the file exists.

Please, help me to understand.

Upvotes: 0

Views: 523

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

use

InputStream istr = context.getResources().getAssets().open("<FILE_PATH/YOUR_FILE_NAME>"); 

instead of

InputStream istr = context.getAssets().open("stanza.xml");

Upvotes: 1

Stefano Ortisi
Stefano Ortisi

Reputation: 5336

if your code is of your activity you can simply write:

InputStream istr =  getResources().getAssets().open("stanza.xml"); 

Upvotes: 1

Related Questions