我解析用的NSXMLParser一個RSS feed,它的做工精細的標題和其他字符串,但要素之一是圖像多數民衆贊成像使用的NSXMLParser與CDATA
<!CDATA <a href="http:image..etc>
如何添加,作爲我的我的表格視圖中的細胞圖像?我會將其定義爲圖像類型嗎?
這是我用來做我的分析是什麼:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [ [NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"description"];
[item setObject:currentDate forKey:@"date"];
[stories addObject:[item copy]];
NSLog(@"adding story: %@", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
是的,有這樣的圖像URL在那裏。如何使用NSXMLParser提取圖像URL? – Xcoder 2009-07-08 02:37:27
查看CDATA部分內的字符串。如果它始終是有效的xml,則在*那個*字符串上觸發另一個NSXMLParser實例。 如果它不是有效的XML,則必須執行Something Else™,例如,手動查找標籤。 – Justicle 2009-07-08 03:32:01