Using XML file resources in Android

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;

/**
 * This example shows how to read xml file from res/xml folder
 *
 * @author FaYnaSoft Labs
 *
 */
public class Main extends Activity {
	private static String APP_TAG = "tag";
	private TextView textView;

	@Override
	public 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() {

			@Override
			public 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.

Code from this article

  • Share/Bookmark

Related posts:

  1. Using files as raw resources in Android
  2. Working with SDCard’s filesystem in Android
  3. Using filesystem in Android
  4. Using assets in Android
  5. Getting SharedPreferences from other application in Android

5 comments to Using XML file resources in Android

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>