Every Time We parse Xml for various purpose. This xml is sample xml formate. some time We need to parse complex xml. Here is one Example , How can parse Complex xml.
***********xml.xml*************
<?xml version=”1.0″ encoding=”utf-8″?>
<CountryCityTown>
<Country>
<Id>202</Id>
<Name>TURKIYE</Name>
<City>
<Id>6747</Id>
<Name>ADANA</Name>
<TownList>
<Town>
<Id>1</Id>
<Name>ADANA</Name>
</Town>
<Town>
<Id>2</Id>
<Name>ALADAG</Name>
</Town>
<Town>
<Id>3</Id>
<Name>CEYHAN</Name>
</Town>
<Town>
<Id>4</Id>
<Name>FEKE</Name>
</Town>
<Town>
<Id>5</Id>
<Name>KARAISALI</Name>
</Town>
<Town>
<Id>6</Id>
<Name>KARATAS</Name>
</Town>
<Town>
<Id>7</Id>
<Name>KOZAN</Name>
</Town>
<Town>
<Id>8</Id>
<Name>POZANTI</Name>
</Town>
<Town>
<Id>9</Id>
<Name>SAIMBEYLI</Name>
</Town>
<Town>
<Id>10</Id>
<Name>TUFANBEYLI</Name>
</Town>
<Town>
<Id>11</Id>
<Name>YUMURTALIK</Name>
</Town>
</TownList>
</City>
<City>
<Id>6748</Id>
<Name>ADIYAMAN</Name>
<TownList>
<Town>
<Id>12</Id>
<Name>ADIYAMAN</Name>
</Town>
</TownList>
</City>
<City>
<Id>6749</Id>
<Name>AFYON</Name>
<TownList>
<Town>
<Id>13</Id>
<Name>AFYON</Name>
</Town>
</TownList>
</City>
<City>
<Id>6750</Id>
<Name>AGRI</Name>
<TownList>
<Town>
<Id>14</Id>
<Name>AGRI</Name>
</Town>
</TownList>
</City>
</Country>
</CountryCityTown>
********************Town.java**************
import java.io.Serializable;
public class Town implements Serializable{
private String Id;
private String name;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
*******Towns.java*************
package com.dbz.readcity;
import java.util.ArrayList;
public class Towns extends ArrayList<Town> {
public Towns() {
super();
}
}
*********CityList.java**********
import java.io.Serializable;
public class CityList implements Serializable{
private String Id;
private String Name;
private Towns Towns;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Towns getTowns() {
return Towns;
}
public void setTowns(Towns towns) {
Towns = towns;
}
}
**************CityBaseParser.java******
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;
public class CityBaseParser extends DefaultHandler{
static String feedUrlString = “http://10.0.2.2/xml/xml.xml”;
// names of the XML tags
static final String COUNTRYCITYTOWN = “CountryCityTown”;
static final String COUNTRY = “Country”;
static final String CITY=”City”;
static final String ID = “Id”;
static final String NAME = “Name”;
static final String TOWN=”Town”;
private CityList cityList;
private Town town;
private Towns towns;
private final URL feedUrl;
public CityBaseParser() {
towns= new Towns();
try {
this.feedUrl = new URL(feedUrlString);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
protected InputStream getInputStream() {
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public List<CityList> parse() {
final List<CityList> allCityList = new ArrayList<CityList>();
RootElement root = new RootElement(COUNTRYCITYTOWN);
Element rootitem = root.getChild(COUNTRY);
Element item=rootitem.getChild(CITY);
item.setStartElementListener(new StartElementListener() {
public void start(Attributes attributes) {
cityList= new CityList();
}
});
item.setEndElementListener(new EndElementListener() {
@Override
public void end() {
allCityList.add(cityList);
}
});
item.getChild(ID).setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
cityList.setId(body);
}
});
item.getChild(NAME).setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
cityList.setName(body);
}
});
item.getChild(“TownList”).setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
towns= new Towns();
Log.i(“TownList”, “START”);
}
});
item.getChild(“TownList”).setEndElementListener(new EndElementListener() {
@Override
public void end() {
Log.i(“TownList”, “END”);
cityList.setTowns(towns);
}
});
item.getChild(“TownList”).getChild(TOWN).getChild(“Id”).setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
town= new Town();
town.setId(body);
Log.i(“ID”, body);
}
});
item.getChild(“TownList”).getChild(TOWN).getChild(“Name”).setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
town.setName(body);
Log.i(“NAME”, body);
towns.add(town);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root
.getContentHandler());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
}
return allCityList;
}
}
***************mainActivity.java******************
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.dbz.readcity.CityBaseParser;
import com.dbz.readcity.CityList;
import com.dbz.readcity.Town;
import com.dbz.readcity.Towns;
public class mainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<CityList> cityLists; CityBaseParser baseParser = new CityBaseParser();
cityLists = baseParser.parse();
}
}