Ios 如何在我的NSXMLParser中实现这个方法来提取图像

Ios 如何在我的NSXMLParser中实现这个方法来提取图像,ios,uiimageview,nsxmlparser,cdata,Ios,Uiimageview,Nsxmlparser,Cdata,我是iOS开发的新手,目前我已经实现了NSXMLparser,但我真的不知道如何分离具有相同名称但不同内容的标记,比如。在一些提要中,这个标记只有摘要,而在另一些提要中,它包含“imgsrc”,我也想提取它。(带或不带CDATA) 我需要抓取图像然后传递到UIImageView的描述标记示例: 罗杰·克雷格·史密斯和特洛伊·贝克将在即将到来的动作游戏中分别扮演蝙蝠侠和小丑;死亡中风被确认为可玩角色 img src=”http://cdn.gsmarena.com/vv/newsimg/13/05

我是iOS开发的新手,目前我已经实现了NSXMLparser,但我真的不知道如何分离具有相同名称但不同内容的标记,比如
。在一些提要中,这个标记只有摘要,而在另一些提要中,它包含“imgsrc”,我也想提取它。(带或不带CDATA)

我需要抓取图像然后传递到UIImageView的描述标记示例:
罗杰·克雷格·史密斯和特洛伊·贝克将在即将到来的动作游戏中分别扮演蝙蝠侠和小丑;死亡中风被确认为可玩角色

img src=”http://cdn.gsmarena.com/vv/newsimg/13/05/samsung-galaxy-s4-active-photos/thumb.jpg“width=“70”height=“92”hspace=“3”alt=“0”border=“0”align=left style=“背景:#333333;填充:0px;边距:0px 4px 0px 0px;边框样式:纯色;边框颜色:#aaaa;边框宽度: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
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName属性:(NSDictionary*)attributeDict
{
element=[elementName copy];
if([elementName IsequalString:@“item”])
{
elements=[[NSMutableDictionary alloc]init];
title=[[NSMutableString alloc]init];
日期=[[NSMutableString alloc]init];
summary=[[NSMutableString alloc]init];
link=[[NSMutableString alloc]init];
img=[[NSMutableString alloc]init];
imageLink=[[NSMutableString alloc]init];
}
如果([elementName IsequalString:@“媒体:缩略图”]){
NSLog(@“缩略图媒体:缩略图:%@”,属性指令);
imageLink=[attributeDict objectForKey:@“url”];
}
如果([elementName IsequalString:@“媒体:内容”]){
NSLog(@“缩略图媒体:内容:%@”,属性指令);
imageLink=[attributeDict objectForKey:@“url”];
}
if([elementName IsequalString:@“enclosure”]){
NSLog(@“缩略图附件%@”,属性指令);
imageLink=[attributeDict objectForKey:@“url”];
}
-(void)解析器:(NSXMLParser*)解析器查找字符:(NSString*)字符串
{
if([元素isEqualToString:@“title”])
{
[标题追加字符串:字符串];
}
else if([元素IsequalString:@“pubDate”])
{
[日期追加字符串:字符串];
}
else if([元素IsequalString:@“description”])
{
[摘要追加字符串:字符串];
}
else if([元素IsequalString:@“媒体:说明”])
{
[摘要追加字符串:字符串];
}
else if([元素IsequalString:@“link”])
{
[链接追加字符串:字符串];
}
else if([元素IsequalString:@“url”]){
[imageLink appendString:string];
}
else if([元素IsequalString:@“src”]){
[imageLink appendString:string];
}
else if([element IsequalString:@“content:encoded”]){
NSString*imgString=[self-getImage:string];
如果(imgString!=nil){
[img appendString:imgString];
NSLog(@“img含量:%@”,img);
}
}
-(NSString*)getImage:(NSString*)htmlString{
NSString*url=nil;
NSScanner*theScanner=[NSScanner scanner-withstring:htmlString];

[扫描仪扫描字符串:@”在您的示例中,您只有两个
description
元素,每个元素中都嵌入了
img
标记。您只需像正常情况一样解析
description
,然后拉出
img
标记(使用正则表达式、使用下面的my
retrieveImageSourceTagsViaRegex
或扫描仪)

注意,如果您不想,您不必以不同的方式处理
CDATA
和非
CDATA
格式副本。虽然
nsxmlparseredelegate
提供了
foundCDATA
例程,但实际上我倾向于不实现该例程。在缺少
foundCDATA
的情况下,标准
foundCharacters
rout
NSXMLParser
的ine将无缝地处理
描述
标记的两种格式副本(带和不带
CDATA

考虑以下假设的XML:

<xml>
    <descriptions>
        <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>
        <description>&lt;img src=&quot;http://cdn.gsmarena.com/vv/newsimg/13/05/samsung-galaxy-s4-active-photos/thumb.jpg&quot; width=&quot;70&quot; height=&quot;92&quot; hspace=&quot;3&quot; alt=&quot;&quot; border=&quot;0&quot; align=left style="background:#333333;padding:0px;margin:0px 4px 0px 0px;border-style:solid;border-color:#aaaaaa;border-width:1px" /&gt; &lt;p&gt;</description>
    </descriptions>
</xml>
这将产生以下结果(请注意,no
CDATA
):

结果=(
{
description=“罗杰·克雷格·史密斯(Roger Craig Smith)和特洛伊·贝克(Troy Baker)将分别在即将到来的动作游戏中扮演蝙蝠侠(蝙蝠侠)和小丑(the Joker);死亡中风(Death Stroke)被确认为可玩角色。

”; imgs=( "http://image.com.com/gamespot/images/2013/139/ArkhamOrigins_29971_thumb.jpg" ); }, { description=“”; imgs=( "http://cdn.gsmarena.com/vv/newsimg/13/05/samsung-galaxy-s4-active-photos/thumb.jpg" ); } )

因此,底线是,只需像平常一样解析XML,不必担心
CDATA
,只需使用
NSScanner
NSRegularExpression
解析图像URL即可。

我很抱歉不够清晰,我的意思是,在某些XML文件中,描述标签在CDATA中包含图像,而其他文件则没有。我上面的描述标记示例来自不同的RSS提要,而不是一个包含两个描述标记的XML文件“图像,但我两者都需要。请在这里查看我的解析器。谢谢,我非常感谢您的帮助。@Edward您根本不必实现
foundCDATA
。如果不实现,标准
foundCharacters
将自动为您解析它,为您正确地从
CDATA
中提取字符(但要消除开头和结尾标记)。特别是如果您有时混合使用
CDATA
,有时不混合使用,只需不实现
foundCDATA
foundCharacters
就可以很好地处理这两个问题
<xml>
    <descriptions>
        <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>
        <description>&lt;img src=&quot;http://cdn.gsmarena.com/vv/newsimg/13/05/samsung-galaxy-s4-active-photos/thumb.jpg&quot; width=&quot;70&quot; height=&quot;92&quot; hspace=&quot;3&quot; alt=&quot;&quot; border=&quot;0&quot; align=left style="background:#333333;padding:0px;margin:0px 4px 0px 0px;border-style:solid;border-color:#aaaaaa;border-width:1px" /&gt; &lt;p&gt;</description>
    </descriptions>
</xml>
@interface ViewController () <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableString *description;
@property (nonatomic, strong) NSMutableArray *results;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSURL *filename = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"xml"];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:filename];
    parser.delegate = self;
    [parser parse];

    // full array of dictionary entries

    NSLog(@"results = %@", self.results);
}

- (NSMutableArray *)retrieveImageSourceTagsViaRegex:(NSString *)string
{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];

    NSMutableArray *results = [NSMutableArray array];

    [regex enumerateMatchesInString:string
                            options:0
                              range:NSMakeRange(0, [string length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

                             [results addObject:[string substringWithRange:[result rangeAtIndex:2]]];
                         }];

    return results;
}

#pragma mark - NSXMLParserDelegate

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.results = [NSMutableArray array];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"description"])
        self.description = [NSMutableString string];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.description)
        [self.description appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"description"])
    {
        NSArray *imgTags = [self retrieveImageSourceTagsViaRegex:self.description];
        NSDictionary *result = @{@"description": self.description, @"imgs" : imgTags};
        [self.results addObject:result];
        self.description = nil;
    }
}

@end
results = (
        {
        description = " <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\">";
        imgs =         (
            "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>";
        imgs =         (
            "http://cdn.gsmarena.com/vv/newsimg/13/05/samsung-galaxy-s4-active-photos/thumb.jpg"
        );
    }
)