Android Xml Resource

This example shows how you can parse a xml file and show the contents.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it AndroidXmlResource.

2.) Create an xml folder into res directory and create and write following into res/xml/eng.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->

<code>0103</code>
MOST COMMON

Yes, no.
Excuse me!
How much?
Hello, bye.
Thanks.
Why, how?
I don’t speak ... (English).
Do you speak ... (French)?
I don’t understand.

<code>0203</code>
COMMUNICATION

Good morning, good evening.
Could you assist me?
You’re speaking too fast.
Could you speak more slowly?
What does this mean?
How do you say ... in (English)?

<code>0303</code>
TRANSPORTATION

I need a taxi.
Drive me to ... (a hotel).
I’m in a hurry.
Does this train go to ...?
Can you drive faster, slower?
Right, left, stop.

 

 

3.) Run for output.

Steps:

1.) Create a project named AndroidXmlResource and set the information as stated in the image.

Build Target: Android 2.2
Application Name: AndroidXmlResource
Package Name: com.example. AndroidXmlResource
Activity Name: AndroidXmlResource
Min SDK Version: 8

2.) Open AndroidXmlResource.java file and write following code there:

 

package com.example.AndroidXmlResource;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidXmlResource extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView myXmlContent = (TextView)findViewById(R.id.eng_xml);
String stringXmlContent;
try {
stringXmlContent = getEventsFromAnXML(this);
myXmlContent.setText(stringXmlContent);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private String getEventsFromAnXML(Activity activity)
throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.eng);
xpp.next();
String text1 =null;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
//do nothing
}
else if(eventType == XmlPullParser.START_TAG)
{
text1=xpp.getName();
}
else if(eventType == XmlPullParser.END_TAG)
{
//do nothing
}
else if(eventType == XmlPullParser.TEXT)
{
if (text1.equalsIgnoreCase("name"))
stringBuffer.append("n "+xpp.getText());
}
eventType = xpp.next();
}
return stringBuffer.toString();
}
}

 

 

3.) Compile and build the project.

Leave a Comment:

5 comments
nakul says March 20, 2012

dear sushant sir,

i want to know what is the use of this code

nakul

Reply
Thomas says March 24, 2012

Good one

Reply
santosh says March 26, 2012

great work good easy

Reply
Sivaraj R says June 13, 2012

Its awesome thing… But how do i retrieve the attribute values and get data with conditionally..

Reply
anand says July 3, 2013

thanks for helping many lives, i would like to access resources from a widget, how to write the code? and what is the interface…

Reply
Add Your Reply