Android XML Parsing Tutorial

Android XML Parsing Tutorial

If you would like package fixed XML together with your application, you need to use an XML useful resource. Simply put the actual XML document inside res/xml/, and you may access it simply by getXml() with a Resources object, providing it a resource ID of R.xml. in addition to the base name of your respective XML file.

  • Therefore, within an activity, if you have an XML file named my_words.xml, you could possibly access it by making use of this coming snippet getResources().getXml(R.xml.my_words)
  • The result of the above method is an instance of an XmlPullParser class. An XML pull parser is actually event-driven : you can keep calling next() method within the parser to obtain the next event.

Simply by looping, testing, and invoking per-element logic, you parse the file.

Creating Layout for XML Parsing Example

res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
  <TextView
    android:id="@+id/selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
  />
  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
  />
</LinearLayout>
res/raw/my_words.xml
<words>
  <word value="Android" />
  <word value="Amazon"/>
  <word value="America"/>
  <word value="Bluetooth" />
  <word value="Brazil"/>
  <word value="Baseball"/>
  <word value="Chrome" />
  <word value="Cricket"/>
  <word value="Docs" />
  <word value="Denamrk"/>
  <word value="Email" />
  <word value="England"/>
  <word value="Facebook" />
  <word value= "France"/>
  <word value="Google" />
  <word value="Gmail"/>
  <word value="Greece"/>
  <word value="Germany"/>
  <word value="Hungary" />
  <word value="Iphone" />
  <word value="India"/>
  <word value="Internet"/>
  <word value="Japan"/>
  <word value="Korea" />
  <word value="London"/>
  <word value="Machintosh" />
  <word value="Nokia" />
  <word value="Norway"/>
  <word value="Orkut" />
  <word value="Olive"/>
  <word value="Picasa" />
  <word value="Singapore" />
  <word value="Talk" />
  <word value="VimalTuts"/>
  <word value="Victoria"/>
  <word value="Windows" />
  <word value="Willington"/>
  <word value="Wipeout"/>
  <word value="White Collar"/>
  <word value="X-Mas"/>
  <word value="Youtube" />
  <word value="Zimbabwe"/>
</words>

Java Code to Configure XML Parsing Example

src/XMLParsingExample.java
package com.vimaltuts.android.myxmlparsingexample;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyXMLParsingExample extends ListActivity {
TextView my_selection;
ArrayList my_items=new ArrayList();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
my_selection=(TextView)findViewById(R.id.selection_tv);
try {
InputStream inStr=getResources().openRawResource(R.raw.my_words);
DocumentBuilder docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=docBuilder.parse(inStr, null);
NodeList my_words=doc.getElementsByTagName("word");
for (int i=0;imy_items.add(((Element)my_words.item(i)).getAttribute("value"));
}
inStr.close();
}
catch (Throwable t) {
Toast.makeText(this, "Exception: "+t.toString(),Toast.LENGTH_LONG).show();
}
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,my_items));
}
public void onListItemClick(ListView parent, View v, int position,long id) {
my_selection.setText(my_items.get(position).toString());
}
}
At this point, within our try…catch block, we have our XmlPullParser as well as loop until the end of your document.
  • Because we’re within total control over the XML file, it really is secure enough to believe there is certainly accurately one attribute.
  • However, in case you where not as secure that the XML is properly described, you may think about exploring the attribute count (getAttributeCount()) and also the name in the attribute (getAttributeName()) prior to blindly assuming the actual 0-index attribute is exactly what you believe it is.

Output Screen of the XML Parser Example


So far we have play with the Android XML Parsing through this Android Tutorial for Beginners : Android XML Parsing Example Tutorial. On the next tutorial we gonna see about the Android SQLite Example

Leave a Reply

Scroll To Top

Foolow Me on Google Plus

VimalTuts on Twitter
62 people follow VimalTuts
MillerHeengmomanmeoluoi_iambgv007rhtShajeelAjatarmlmanognya