Android Tutorial

(avery) #1
Android Tutorial 151






Now you can access this XML file as a resource programmatically in
the following manner:

XmlResourceParser myPets =
getResources().getXml(R.xml.my_pets);


Finally, to prove this is XML, here’s one way you might churn
through the XML and extract the information:

import org.xmlpull.v1.XmlPullParserException;
import android.content.res.XmlResourceParser;
...
int eventType = -1;
while (eventType != XmlResourceParser.END_DOCUMENT) {
if(eventType == XmlResourceParser.START_DOCUMENT) {
Log.d(DEBUG_TAG, “Document Start”);
} else if(eventType == XmlResourceParser.START_TAG) {
String strName = myPets.getName();
if(strName.equals(“pet”)) {
Log.d(DEBUG_TAG, “Found a PET”);
Log.d(DEBUG_TAG,“Name:“+myPets.getAttributeValue(
null,“name”));
Log.d(DEBUG_TAG, “Species:“+myPets.getAttributeValue(
null,“type”));
}
}
eventType = myPets.next();
}
Log.d(DEBUG_TAG, “Document End”);


Working with Raw Files

Your application can also include raw files as part of its resources.
For example, your application might use raw files such as audio
Free download pdf