I have an xml file on a web page http://api.androidhive.info/music/music.xml on my application I use
static final String URL = "http://api.androidhive.info/music/music.xml";
String xml = parser.getXmlFromUrl(URL);
works fine but know I have put this file in my Android resource folder. So how do I get my file from android drawable folder like this R.drawble.music.xml
instead of web page. http://api.androidhive.info/music/music.xml
Put the xml into /res/raw
folder.
Then use :
InputStream is=getResources().openRawResource(R.raw.my_xml);
Then use this parser XML PULL PARSER
XmlPullParser xpp=XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(is,"your-encoding");
Thanks.
Try this:
String file = "res/drawable/music.xml";
XmlResourceParser parser = getContext().getAssets().openXmlResourceParser(file);
or in Kotlin:
val file = "res/drawable/music.xml"
val parser = context.assets.openXmlResourceParser(file)
It worked for me. And then you can look for examples of XmlPullParser
to know how to use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.