2013-04-18 88 views
0

我試圖獲取包含在雅虎RSS feed的media:content標籤中的url。對於didStartElement(),foundCharacters()和didEndElement()的代碼如下:從雅虎rss feed解析媒體內容源xcode 4.5 ios

- (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; 
} 

我打印所分析的數據日誌。圖像顯示爲空。其他一切,例如:標題和說明正在得到適當的。我該如何解決它?可能需要在didEndElement方法中完成某些操作。但我不知道是什麼。自昨天以來,我一直在絞盡腦汁。請幫忙!!

我做出didStartElement()的一些變化:)

if ([elementName isEqualToString:kItemImage]) { 
      NSString *urlString = attributeDict[@"url"]; 
      if(urlString){ 
       [dict setObject:urlString forKey:@"img"]; 
       NSLog(@"%@",urlString); 
       mString = [NSString stringWithFormat:urlString]; 

和didEndElement(一些變化:

else if([currentElementData rangeOfString:@"media:content"].location){ 
      if(itemCollection!=nil){ 
      // [currentElementData appendString:dict]; 
       itemCollection.titleImage = mString; 
      } 
     } 

mString已被宣佈爲一個可變的字符串。圖像現在得到解析。他們的網址正在日誌中顯示。現在的問題是,最後一張圖片重複兩次。這可能是因爲最後一個Feed沒有圖片,所以它使用的是前一張。如何解決這個問題?我的意思是,可以有多個沒有圖像的提要。在這種情況下,它實際上應該顯示null而不是之前圖像的URL。

回答

0

我不`噸以及瞭解雅虎RSS飼料,但我希望它會幫助你.....

只是試訓的這....

- (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); 
    } 
} 
}