parsing XML String using XmlPullParser in Android



i am trying to parse the following XML



<ArrayOfArtist xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:xsd="http://ift.tt/tphNwY" xmlns="http://tempuri.org/">
<Artist>
<place>1</place>
<Name>Mr Guy</Name>
</Artist>
<Artist>
<place>2</place>
<Name>Mr Buddy</Name>
</Artist>
<Artist>
<place>3</place>
<Name>Mr Friend</Name>
</Artist>
</ArrayOfArtist>


this is what i tried. Album is a class having String name and place. the output should be name:Mr Guy place:1 name:Mr Buddy place:2 name: Mr Friend place:3



ArrayList<Album> products = null;
Album a=null;
while(xpp.getEventType() !=XmlPullParser.END_DOCUMENT){
String name=null;
switch (xpp.getEventType()){
case XmlPullParser.START_DOCUMENT:
products = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = xpp.getName();
Log.e(TAG,"name is"+xpp.getName());
if (xpp.getName().equals("ArrayOfArtist")){
Log.e(TAG,"herehere");
a = new Album();
} else if (a != null){
if (name.equals("Artist")){
Log.e(TAG,"artart");
}
if (name.equals("place")){
Log.e(TAG,"placeplace");
a.place = xpp.nextText();
}
else if (name.equals("name")){
Log.e(TAG,"namename");
a.name = xpp.nextText();
}
}
break;

case XmlPullParser.END_TAG:
name = xpp.getName();
if (name.equalsIgnoreCase("ArrayOfArtist") && a != null){

products.add(a);
}
}
xpp.next();
}
printProducts(products);
}
catch (Exception e){

}

}
}
private void printProducts(ArrayList<Album> products)
{
String content = "";
Iterator<Album> it = products.iterator();
while(it.hasNext())
{
Log.e(TAG,"hete");
Album currProduct = it.next();
content = content + "name :" + currProduct.name + "\n";
content = content + "place :" + currProduct.place + "\n";

}

TextRecieved.setText("content is: "+content);
}


the output i am getting is: content is:name:null place:3


No comments:

Post a Comment