Hi friends. I am now showing you how to parse XML in Android using SAXParser.
XML is the simplest and easiest way to send data over the internet. When we are going to hit any HTTP request (make a HTTP connection to get data) we get the data in XML Format.
To parse the XML we have the XML data, make a new raw folder in the res folder in the Android application.
And make the XML file in it and paste the code given below.
- <dist>
- <item>
- <country>AFGHANISTAN</country>
- <alpha2>AF</alpha2>
- <alpha3>AFG</alpha3>
- <value>+93</value>
- </item>
- <item>
- <country>LAND ISLANDS</country>
- <alpha2>AX</alpha2>
- <alpha3>ALA</alpha3>
- <value>+248</value>
- </item>
- <item>
- <country>ALBANIA</country>
- <alpha2>AL</alpha2>
- <alpha3>ALB</alpha3>
- <value>+355</value>
- </item>
- <item>
- <country>ALGERIA</country>
- <alpha2>DZ</alpha2>
- <alpha3>DZA</alpha3>
- <value>+213</value>
- </item>
- <item>
- <country>AMERICAN SAMOA</country>
- <alpha2>AS</alpha2>
- <alpha3>ASM</alpha3>
- <value>+1684</value>
- </item>
- <item>
- <country>ANDORRA</country>
- <alpha2>AD</alpha2>
- <alpha3>AND</alpha3>
- <value>+376</value>
- </item>
- </dist>
The following is the Model class for the ArrayList:
- package com.ravi.model;
- public class CountryCodeModel {
- String countryName;
- String countryCode;
- String countryShortCode;
- public String getCountryName() {
- return countryName;
- }
- public String getCountryCode() {
- return countryCode;
- }
- public String getCountryShortCode() {
- return countryShortCode;
- }
- public void setCountryName(String countryName) {
- this.countryName = countryName;
- }
- public void setCountryCode(String countryCode) {
- this.countryCode = countryCode;
- }
- public void setCountryShortCode(String countryShortCode) {
- this.countryShortCode = countryShortCode;
- }
- }
The following is the XMLHandler class that extends the DefaultHandler and overrides the three methods:
- public class XMLHandler extends DefaultHandler {
- boolean bCountryCode = false;
- boolean bCountryName = false;
- boolean bCountryShortCode = false;
- ArrayList<CountryCodeModel> countryCodeList = new ArrayList<CountryCodeModel>();
- private CountryCodeModel countryCode = null;
- public ArrayList<CountryCodeModel> getList() {
- return countryCodeList;
- }
- }
The override methods are the following:
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- }
All these methods are useful for parsing the XML in the startElement method. We just check the tag name and make the boolean variable value true to make the Model class object useful in the characters method.
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if (qName.equalsIgnoreCase("value")) {
- bCountryCode = true;
- } else if (qName.equalsIgnoreCase("country")) {
- bCountryName = true;
- countryCode = new CountryCodeModel();
- } else if (qName.equalsIgnoreCase("alpha3")) {
- bCountryShortCode = true;
- }
- }
Here the characters method code is given below.
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if (bCountryCode) {
- bCountryCode = false;
- countryCode.setCountryCode(new String(ch, start, length));
- } else if (bCountryName) {
- bCountryName = false;
- countryCode.setCountryName(new String(ch, start, length));
- } else if (bCountryShortCode) {
- bCountryShortCode = false;
- countryCode.setCountryShortCode(new String(ch, start, length)); }
- }
The EndElement method checks the last tag name. If they are there then add the model class object into the list.
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if (qName.equalsIgnoreCase("value")) {
- countryCodeList.add(countryCode);
- }
- }
Now focus on the activity in which we write the code for parsing the XML. Make the oncreate method in the activity and make the SAXParserFactory object and also make the SAXParser object and make the XMLHandler class object.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main_activity);
- ListView listView = (ListView) findViewById(R.id.listView);
- try {
- SAXParserFactory parserFactory = SAXParserFactory.newInstance();
- SAXParser parser = parserFactory.newSAXParser();
- XMLHandler handler = new XMLHandler();
- parser.parse(getResources()
- .openRawResource(R.raw.country_name_isd2), handler);
- countryCodeList = handler.getList();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (NotFoundException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- CountryListAdapter adapter = new CountryListAdapter(
- MainHandlerActivity.this, countryCodeList);
- listView.setAdapter(adapter);
- }
The last of the method adapter code is there, to just show the parse data in the List format.
Now here is the complete code of the XMLHandler class and MainHandlerActiviy.
XMLHandler.java.
- package com.ravi.xmlhandler;
- import java.util.ArrayList;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import com.ravi.model.CountryCodeModel;
-
-
-
-
- public class XMLHandler extends DefaultHandler {
- boolean bCountryCode = false;
- boolean bCountryName = false;
- boolean bCountryShortCode = false;
- ArrayList<CountryCodeModel> countryCodeList = new ArrayList<CountryCodeModel>();
- private CountryCodeModel countryCode = null;
- public ArrayList<CountryCodeModel> getList() {
- return countryCodeList;
- }
- @Override
-
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if (qName.equalsIgnoreCase("value")) {
- bCountryCode = true;
- } else if (qName.equalsIgnoreCase("country")) {
- bCountryName = true;
- countryCode = new CountryCodeModel();
- } else if (qName.equalsIgnoreCase("alpha3")) {
- bCountryShortCode = true;
- }
- }
- @Override
-
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if (qName.equalsIgnoreCase("value")) {
- countryCodeList.add(countryCode);
- }
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if (bCountryCode) {
- bCountryCode = false;
- countryCode.setCountryCode(new String(ch, start, length));
- } else if (bCountryName) {
- bCountryName = false;
- countryCode.setCountryName(new String(ch, start, length));
- } else if (bCountryShortCode) {
- bCountryShortCode = false;
- countryCode.setCountryShortCode(new String(ch, start, length)); }
- }
- }
MainHandlerActivity.java
- package com.ravi.xmlhandler;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.SAXException;
- import com.example.xmlhandlerdemo.R;
- import com.ravi.adapter.CountryListAdapter;
- import com.ravi.model.CountryCodeModel;
- import android.app.Activity;
- import android.content.res.Resources.NotFoundException;
- import android.os.Bundle;
- import android.view.Window;
- import android.widget.ListView;
- public class MainHandlerActivity extends Activity {
- ArrayList<CountryCodeModel> countryCodeList = new ArrayList<CountryCodeModel>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main_activity);
- ListView listView = (ListView) findViewById(R.id.listView);
- try {
- SAXParserFactory parserFactory = SAXParserFactory.newInstance();
- SAXParser parser = parserFactory.newSAXParser();
- XMLHandler handler = new XMLHandler();
- parser.parse(getResources()
- .openRawResource(R.raw.country_name_isd2), handler);
- countryCodeList = handler.getList();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (NotFoundException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- CountryListAdapter adapter = new CountryListAdapter(
- MainHandlerActivity.this, countryCodeList);
- listView.setAdapter(adapter);
- }
- }
Main_activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/listView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:divider="@android:color/black"
- android:dividerHeight="1dp" >
- </ListView>
- </LinearLayout>
List_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="5dp" >
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:padding="10dp"
- android:text="Country name"
- android:textColor="@android:color/black" />
- <TextView
- android:id="@+id/code"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:padding="10dp"
- android:text="Country Code"
- android:textColor="@android:color/black" />
- <TextView
- android:id="@+id/shortName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@id/name"
- android:layout_marginTop="5dp"
- android:padding="10dp"
- android:text="Short Name"
- android:textColor="@android:color/black" />
- </RelativeLayout>
The following is a snapshort of the result: