【问题标题】:how to access images by xml parsing?如何通过xml解析访问图像?
【发布时间】:2013-05-30 20:01:52
【问题描述】:

在我的 ios 应用程序中,我正在尝试从以下 XMl 文件访问图像 如何获取<description> 标签中的图片?

<channel>
          <title>Gomolo Photo Gallery</title>
          <link>http://www.gomolo.com/</link>
          <description>Movie stills and Celebrity Wallpapers on Gomolo.com</description>
          <language>en</language>
          <lastBuildDate>6/4/2013 10:20:40 AM</lastBuildDate>
          <image>
                 <url>http://img1.gomolo.com/images/logos/gomolo_M.jpg</url>
                 <title>Gomolo Photo Gallery</title>
                 <link>http://www.gomolo.com/</link>
             </image>
          <ttl>360</ttl>
          <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/GomoloPhoto"></atom10:link>
          <feedburner:info uri="gomolophoto"></feedburner:info>
          <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/"></atom10:link>
          <feedburner:emailServiceId>GomoloPhoto</feedburner:emailServiceId>
          <feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname>
          <item>
                 <guid isPermaLink="false">809390</guid>
                 <link>http://feedproxy.google.com/~r/GomoloPhoto/~3/dUyLtllEQUA/photos-rituparna-sengupta-at-the-first-look-launch-of-bengali-movie-a-political-murder</link>
                 <title></title>
                 <description>
Rituparna Sengupta at the first look launch of Bengali movie 'A Political Murder'</description>
                     <a10:updated>6/4/2013 9:55:00 AM</a10:updated>
                     <feedburner:origLink>http://www.gomolo.com/809390/photos-rituparna-sengupta-at-the-first-look-launch-of-bengali-movie-a-political-murder</feedburner:origLink>
                 </item>
              <item>
                     <guid isPermaLink="false">809483</guid>
                     <link>http://feedproxy.google.com/~r/GomoloPhoto/~3/arNMUvINuok/photos-will-smith-jaden-smith-bruce-willis-at-the-premiere-of-hollywood-movie-after-earth</link>
                     <title></title>
                     <description>
Will Smith, Jaden Smith & Bruce Willis at the premiere of Hollywood movie 'After Earth'</description>
                     <a10:updated>6/4/2013 9:53:00 AM</a10:updated>
                     <feedburner:origLink>http://www.gomolo.com/809483/photos-will-smith-jaden-smith-bruce-willis-at-the-premiere-of-hollywood-movie-after-earth</feedburner:origLink>
                 </item>

我正在尝试这个

NSMutableData *data=[[NSMutableData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://feeds.feedburner.com/GomoloPhoto"]];
GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];

//specify path
NSArray *links=[doc nodesForXPath:@"//channel/item/description" error:nil];

//load xml data
for (GDataXMLElement *link in links ) {
    NSURL *url=[NSURL URLWithString: link.stringValue];
    [array addObject:url];
}
NSLog(@"array %@",[array objectAtIndex:0]);
NSData *data1=[NSData dataWithContentsOfURL:[array objectAtIndex:0]];
_hotPicsImageView1.image=[UIImage imageWithData:data1];
NSData *data2=[NSData dataWithContentsOfURL:[array objectAtIndex:1]];
_hotPicsImageView2.image=[UIImage imageWithData:data2];
NSData *data3=[NSData dataWithContentsOfURL:[array objectAtIndex:2]];
_hotPicsImageView3.image=[UIImage imageWithData:data3];

但由于某种原因,图像视图从我的默认图像变为空白, 如果您检查&lt;description&gt; 标签,您会发现它包含图像和一些文本。 我如何访问图像文件? 任何人都可以建议我这样做的正确方法吗?

请在任何在线 xml 查看器中查看此 url

【问题讨论】:

    标签: ios web-services parsing xml-parsing gdataxml


    【解决方案1】:

    图片地址在image-&gt;ulr标签中

     <image>
    <url>http://img1.gomolo.com/images/logos/gomolo_M.jpg</url>
    

    不在&lt;discription&gt;标签中

    编辑:您共享的 xml 中没有有效的图像 url

    【讨论】:

    • 不,那是 Gomolo 的徽标图像,描述标签中有图像。我通过在线 xml 查看器检查了
    • 在上面提供的 xml 中“Gomolo.com 上的电影剧照和名人壁纸”是第一个描述标签,它肯定不是 url
    • 您可以在任何在线 xml 查看器上查看此网址:- feeds.feedburner.com/GomoloPhoto。我正在努力从描述标签中过滤图像。
    【解决方案2】:

    测试 XPath 的好工具http://www.xpathtester.com/

    link

    也许这对你有帮助 - Parse <img> tag that lies within the <description></description> of an rss2 feed item

    NSMutableData *data=[[NSMutableData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://feeds.feedburner.com/GomoloPhoto"]];
        GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
    
        //specify path
        NSArray *links=[doc nodesForXPath:@"//channel/item/description" error:nil];
    
        NSMutableArray *array = [NSMutableArray array];
    
        //load xml data
        for (GDataXMLElement *link in links ) {
    
            NSString *descWithLink = [link stringValue];
    
            NSMutableArray *substrings = [NSMutableArray array];
    
            NSScanner *scanner = [NSScanner scannerWithString:descWithLink];
            [scanner scanUpToString:@"src=" intoString:nil]; 
            while(![scanner isAtEnd]) {
                NSString *substring = nil;
                [scanner scanString:@"src" intoString:nil]; 
                if([scanner scanUpToString:@"\" " intoString:&substring]) {
                    [substrings addObject:[substring substringFromIndex:2]];
                }
                [scanner scanUpToString:@"src=" intoString:nil]; 
            }
    
            NSURL *url=[NSURL URLWithString: substrings[0]];
            [array addObject:url];
        }
        NSLog(@"array %@",[array objectAtIndex:0]);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2023-03-17
    • 2010-11-22
    • 2012-03-14
    • 2012-03-23
    相关资源
    最近更新 更多