In Android application you can use /res/xml XML files. These files are different than other Android resources. They are different from raw files,you don’t use a stream to access them because they are compiled into an efficient binary form when deployed,and they are different from other resources and they can be of any custom XML structure. I am going to show example how you can access these files and read data from them.
At first I will show simple xml file which called cars.xml and it is located at /res/xml folder:
<car position="1" brand="Mercedes-Benz" /><car position="2" brand="BMW" /><car position="3" brand="Audi" />
As you can see it is very simple xml structure. Now let’s look how we can access this xml file from code:
XmlPullParser parser = getResources().getXml(R.xml.cars);
And after that you can use this parser for getting data.
Example:
import org.xmlpull.v1.XmlPullParser;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;public class Main extends Activity{private static String APP_TAG = "tag";private TextView textView;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);textView = (TextView) findViewById(R.id.textField);findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){XmlPullParser parser = getResources().getXml(R.xml.cars);StringBuilder stringBuilder = new StringBuilder();try{while (parser.next() != XmlPullParser.END_DOCUMENT){String name = parser.getName();String position = null;String brand = null;if((name != null) &&name.equals("car")){int size = parser.getAttributeCount();for(int i = 0;i < size;i++){String attrName = parser.getAttributeName(i);String attrValue = parser.getAttributeValue(i);if((attrName != null) &&attrName.equals("position")){position = attrValue} else if ((attrName != null) &&attrName.equals("brand")){brand = attrValue}}if((position != null) &&(brand != null)){stringBuilder.append(position + "," + brand + "\n")}}textView.setText(stringBuilder.toString())}}catch(Exception e){Log.e(APP_TAG,e.getMessage())}}})}}This is simple example which read data from cars.xml and set it to the TextView component.
Was it useful for you? Leave a comment.
Feel free to request a new examples.
No related posts.

Thanks! Exactly what is was looking for. And the coincidence is that I’m using it for a car application
I am glad that this post can help you.
OK. And if I want to get images
….
???
Hi Vladimir. I recommend to use assets,drawable or raw folders for getting images.
Great! Thank you!
Simple and helpful.
It’s unbelievable how hard it is to find good help with simple tasks =)
Thanks.