xml - parsing media content from yahoo rss feed xcode 4.5 ios -
i'm trying obtain url contained in media:content tag of yahoo rss feed. code didstartelement(), foundcharacters() , didendelement() given below:
- (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname attributes:(nsdictionary *)attributedict { if ([elementname isequaltostring:kchannelelementname]) { channel = [[channel alloc] init]; dict=[[nsmutabledictionary alloc] init]; [channel setitemcollectionarray:[[nsmutablearray alloc] init]]; return ; } if ([elementname isequaltostring:kitemelementname]) { itemcollection=[[itemdatacollection alloc] init]; return ; } if ([elementname isequaltostring:ktitleelementname]) { return ; } if([elementname isequaltostring:kitemdescription]){ return ; } if ([elementname isequaltostring:kitemimage]) { nsstring *urlstring = attributedict[@"url"]; if(urlstring){ [dict setobject:urlstring forkey:@"img"]; } return ; } } - (void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)string { if (currentelementdata == nil) { self.currentelementdata = [[nsmutablestring alloc] init]; } [currentelementdata appendstring:string]; } - (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname { if ([elementname isequaltostring:kchannelelementname]) { [channelcollection addobject:channel]; nslog(@"channel are***********%@",channelcollection); for(itemdatacollection *mydata in channel.itemcollectionarray){ nslog(@"___%@ <><><><><> desc \n %@ <><><><><><> img \n %@",mydata.title,mydata.itemdescription,mydata.titleimage); } channel =nil; } else if ([elementname isequaltostring:kitemelementname]) { [[channel itemcollectionarray] addobject:itemcollection]; itemcollection=nil; } else if ([elementname isequaltostring:ktitleelementname]) { if(itemcollection==nil){ channel.title=currentelementdata; } else{ itemcollection.title=currentelementdata; } } else if ([elementname isequaltostring:kpubdate]) { channel.pubdate=currentelementdata; } else if ([elementname isequaltostring: kitemdescription]) { if(itemcollection!=nil){ itemcollection.itemdescription=currentelementdata; } } else if([elementname isequaltostring:@"media:content"]){ if(itemcollection!=nil){ itemcollection.titleimage = currentelementdata; } } // [currentelementdata release]; self.currentelementdata = nil; } i'm printing parsed data log. image showing null. else eg;title , description getting obtained properly. how can fix it? needs done in didendelement method. don't know what. i've been racking brains on since yesterday. please help!!
i made changes in didstartelement():
if ([elementname isequaltostring:kitemimage]) { nsstring *urlstring = attributedict[@"url"]; if(urlstring){ [dict setobject:urlstring forkey:@"img"]; nslog(@"%@",urlstring); mstring = [nsstring stringwithformat:urlstring]; and change in didendelement():
else if([currentelementdata rangeofstring:@"media:content"].location){ if(itemcollection!=nil){ // [currentelementdata appendstring:dict]; itemcollection.titleimage = mstring; } } mstring has been declared mutable string. images getting parsed now. urls getting displayed in log. problem now, last image repeated twice.. because last feed not have image, using previous one. how fix that? mean, there can multiple feeds without images. in case, should display null rather url of previous image.
i don`t know yahoo rss feed hope you.....
just tryout this....
- (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname attributes:(nsdictionary *)attributedict { currentelement = [elementname copy]; if ([elementname isequaltostring:@"item"]) { item = [[nsmutabledictionary alloc] init]; currenttitle = [[nsmutablestring alloc] init]; currentdate = [[nsmutablestring alloc] init]; currentsummary = [[nsmutablestring alloc] init]; currentlink = [[nsmutablestring alloc] init]; currentimage = [[nsmutablestring alloc] init]; } else if([elementname isequaltostring:@"media:content"]) { currentimage = [attributedict objectforkey:@"img src"]; nslog(@"url:%@", currentimage); } } }
Comments
Post a Comment