1

我正在使用 NSXMLParser 解析托管的 XML 文件,当我单击刷新按钮时,虽然 XML 内容已更改,但 NSXMLParser 内容并未更新,我猜 NSXMLParser 有我的 XML 文件的缓存,如何清除 NSXMLParser 缓存?

添加到列表后,我应该在 didEndElement 中释放 currentTitle 、 currentLink 和 item 吗?

代码:

NSMutableDictionary *item;
NSString * currentElement;
NSMutableString * currentTitle, * currentLink;
NSMutableArray *list;


- (void)parseXMLFileAtURL:(NSString *)URL { 

    NSURL *xmlURL = [NSURL URLWithString:URL];
    NSXMLParser *rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
    [rssParser parse];
    [rssParser release];
}


- (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];
        currentLink = [[NSMutableString alloc] init];
    }
}



- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if ([currentElement isEqualToString:@"title"]) {
        [currentTitle appendString:string];
    } else if ([currentElement isEqualToString:@"url"]) {
        [currentLink appendString:string];
    }

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 

    if ([elementName isEqualToString:@"item"]) { 

        [item setObject:currentLink forKey:@"link"];
        [item setObject:currentTitle forKey:@"title"];
        [list addObject:[item copy]];

    }
}


- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [tableView reloadData];
}
4

0 回答 0