Reputation: 7092
I want to load data from an XML file and assign that data to variables in my Android app.
After doing some research on the various XML parsers available to Android, I figure the best parser method to use would be SAX.
I've read through a bunch of posts about reading XML from Android however I can't find one that uses a local file in the 'res' folder.
All the tutorials I've found are loading the XML file via a URL.
Is there a way to load the XML from the res folder?
Thanks
Upvotes: 2
Views: 6872
Reputation: 437
you cant use a xml file in raw folder. because this error occurred : "Expected resource of type raw." but u can easily place this xml file in assets folder and use it with :
is = getAssets().open("myFedd.xml") ;
Upvotes: 0
Reputation: 3666
You could look at my past question regarding the same issue: How to use DOM to read XML within Android applications correctly?
From my experience I've copied the xml file into the res/raw
folder and was able to read it correctly there using res.openRawResource(R.raw.the_id)
. You can load it from the res/layout
folder but some of the information when you parse it may be in binary, which wouldn't be ideal.
Upvotes: 1
Reputation: 72321
Use :
InputStream is=getResources().openRawResource(R.raw.my_xml);
to get an input stream to your XML, and then use XMLPullParser to parse the file :
XmlPullParser xpp=XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(is,"your-encoding");
Upvotes: 1
Reputation: 26971
Put the xml into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you instance of XML parser.
Upvotes: 2