将NSXMLParser与CDATA一起使用

时间:2009-07-08 01:55:59

标签: iphone objective-c nsxmlparser cdata

我正在使用NSXMLParser解析RSS提要,它对标题和其他字符串工作正常,但其中一个元素是像

的图像
<!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];
}

}

2 个答案:

答案 0 :(得分:21)

您可以使用

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}

答案 1 :(得分:4)

CDATA的观点是其中的任何内容都不被视为XML文档的一部分。 因此,如果您在CDATA中有标签,解析器将忽略它们。

我猜这个CDATA在描述元素中。因此,您需要手动或通过解析器的另一个实例从“description”元素中提取标记。