我是 iOS 开发的新手,此时我已经实现了 NSXMLparser ,但我真的不知道如何分隔具有相同名称但内容不同的标签,例如<description>
. 在某些提要中,此标签只有摘要,而在其他提要中,包含我也想提取的“img src”。(有或没有 CDATA)
Example of description tags wich i need to grab the images and then pass to my UIImageView:
<description><![CDATA[ <p>Roger Craig Smith and Troy Baker to play Batman and the Joker respectively in upcoming action game; Deathstroke confirmed as playable character. </p><p><img src="http://image.com.com/gamespot/images/2013/139/ArkhamOrigins_29971_thumb.jpg"
<description><img src="http://cdn.gsmarena.com/vv/newsimg/13/05/samsung-galaxy-s4-active-photos/thumb.jpg" width="70" height="92" hspace="3" alt="" border="0" align=left style="background:#333333;padding:0px;margin:0px 4px 0px 0px;border-style:solid;border-color:#aaaaaa;border-width:1px" /> <p>
我认为@Rob示例解决了我的情况,但我不知道如何在我的 NSXMLParser 中包含(如下所述)来分离数据和图像。我只能获取此解析器上的数据(摘要)。
我的 NSXMLParser:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
element = [elementName copy];
if ([elementName isEqualToString:@"item"])
{
elements = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
date = [[NSMutableString alloc] init];
summary = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
img = [[NSMutableString alloc] init];
imageLink = [[NSMutableString alloc]init];
}
if([elementName isEqualToString:@"media:thumbnail"]) {
NSLog(@"thumbnails media:thumbnail: %@", attributeDict);
imageLink = [attributeDict objectForKey:@"url"];
}
if([elementName isEqualToString:@"media:content"]) {
NSLog(@"thumbnails media:content: %@", attributeDict);
imageLink = [attributeDict objectForKey:@"url"];
}
if([elementName isEqualToString:@"enclosure"]) {
NSLog(@"thumbnails Enclosure %@", attributeDict);
imageLink = [attributeDict objectForKey:@"url"];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([element isEqualToString:@"title"])
{
[title appendString:string];
}
else if ([element isEqualToString:@"pubDate"])
{
[date appendString:string];
}
else if ([element isEqualToString:@"description"])
{
[summary appendString:string];
}
else if ([element isEqualToString:@"media:description"])
{
[summary appendString:string];
}
else if ([element isEqualToString:@"link"])
{
[link appendString:string];
}
else if ([element isEqualToString:@"url"]) {
[imageLink appendString:string];
}
else if ([element isEqualToString:@"src"]) {
[imageLink appendString:string];
}
else if ([element isEqualToString:@"content:encoded"]){
NSString *imgString = [self getImage:string];
if (imgString != nil) {
[img appendString:imgString];
NSLog(@"Content of img:%@", img);
}
}
-(NSString *) getImage:(NSString *)htmlString {
NSString *url = nil;
NSScanner *theScanner = [NSScanner scannerWithString:htmlString];
[theScanner scanUpToString:@"<img" intoString:nil];
if (![theScanner isAtEnd]) {
[theScanner scanUpToString:@"src" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
[theScanner scanUpToCharactersFromSet:charset intoString:nil];
[theScanner scanCharactersFromSet:charset intoString:nil];
[theScanner scanUpToCharactersFromSet:charset intoString:&url];
}
return url;
}
@end