Saturday, 27 September 2014

Parsing XML using PullParser



I am trying to parse an XML file using PullParser but I am not doing something right. I want to grab a few of the fields and store them in an object and then store that object in an ArrayList. I think it may be a logic issue with how I am determining which tags to pull text from but I cannot seem to figure it out.


The XML has two <title> tags that needs to be ignored and one <description> tag that needs to be ignored in the top of the file and I think this may be at least part of my issue.


The link to the XML:


http://ift.tt/YYwVX8



while(event != XmlPullParser.END_DOCUMENT){

switch(event){
case XmlPullParser.START_TAG:
if(parser.getName().equals("title") && !parser.nextText().trim().contains("BBC News -")){
newsItem = new NewsItems();
newsItem.setTitle(parser.nextText().trim());
Log.d("Title", newsItem.getTitle());
}else if(parser.getName().equals("description") && !parser.nextText().trim().contains("The latest stories")){
newsItem.setDescritpion(parser.nextText().trim());
Log.d("description", parser.nextText().trim());
}else if(parser.getName().equals("pubDate")){
newsItem.setPubDate(parser.nextText().trim());
}else if(parser.getName().equals("media:thumbnail")){
if(parser.getAttributeValue(null, "width").equals("66")){
newsItem.setThmnSmall(parser.getAttributeValue(null, "url").trim());
Log.d("small thumbnail", parser.getAttributeValue(null, "url").trim());
}
}else if(parser.getName().equals("media:thumbnail")){
if(parser.getAttributeValue(null, "width").equals("144")){
newsItem.setThmnLarge(parser.getAttributeValue(null, "url").trim());
Log.d("large thumbnail", parser.getAttributeValue(null, "url").trim());
}
}

break;
case XmlPullParser.END_TAG:
if(parser.getName().equals("title")){
newsList.add(newsItem);
newsItem = null;
}

default:
break;
}
event = parser.next();
}
return newsList;

No comments:

Post a Comment