我製作了一個簡單的rss閱讀器。該應用程序在數組中加載一個xml原子文件。深入研究rss閱讀器iphone
現在我已經加入分類我的Atom feed,這是第一個數組
在加載哪些是添加編程向下鑽取功能的最佳途徑。 現在只有類別被加載到數組中並顯示。
這是實現代碼
.....
loading xml file <snip>
.....
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i)", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (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:@"entry"]) {
// clear out our story item caches...
Categoryentry = [[NSMutableDictionary alloc] init];
currentID = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentContent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"entry"]) {
// save values to an entry, then store that item into the array...
[Categoryentry setObject:currentTitle forKey:@"title"];
[Categoryentry setObject:currentID forKey:@"id"];
[Categoryentry setObject:currentSummary forKey:@"summary"];
[Categoryentry setObject:currentContent forKey:@"content"];
[categories addObject:[Categoryentry copy]];
NSLog(@"adding category: %@", 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:@"id"]) {
[currentID appendString:string];
} else if ([currentElement isEqualToString:@"summary"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"content"]) {
[currentContent appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
NSLog(@"all done!");
NSLog(@"categories array has %d entries", [categories count]);
[newsTable reloadData];
}
非常感謝你。 這就是我正在尋找的。 – bing 2009-09-22 17:29:20