I place the local XML file into res/raw
and then loads it into InputStreamer
object. It works fine and I am able to parse its content.
When I place the same XML into res/xml
, I get XmlPullParserException
saying it can't find the START tag.
I use this code to fill InputStream
object:
InputStream is = getResources().openRawResource(R.raw.data);
and this line to load the XML from /res/xml
:
InputStream is = context.getResources().openRawResource(R.xml.data);
Why is this happening? If the 2nd approach is the wrong one, what is then the purpose of res/xml
?
getXML()
doesn't return an InputStream
so I'm not sure how your code compiles. It returns an XMLResourceParser
.
http://developer.android.com/reference/android/content/res/Resources.html#getXml(int)
Return an XmlResourceParser through which you can read a generic XML resource for the given resource ID.
The purpose of /res/xml
is it's a handy place to store XML and later parse it! Sometimes you don't need an InputStream
:)
You can read it like this:
void parseXml(int xmlId){
Resources res = context.getResources();
//Xml parserer
XmlResourceParser parser = res.getXml(xmlResource);
while (parser.next() != XmlPullParser.END_DOCUMENT) {
//A start tag is reached
if(parser.getEventType() == XmlPullParser.START_TAG){
//Which tag is
if(parser.getName().equalsIgnoreCase("Name"){
//Do something when a <Name> tag is reached
//and so on ...
}
}
}
}
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.