首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从NSXMLDocument获取值

从NSXMLDocument获取值
EN

Stack Overflow用户
提问于 2010-09-01 04:36:07
回答 1查看 1.4K关注 0票数 0

我正在使用API上传图像,并且需要从如下所示的XML输出中获取上传的图像URL (image_link

代码语言:javascript
复制
<?xml version="1.0" encoding="iso-8859-1"?><imginfo xmlns="http://ns.imageshack.us/imginfo/7/" version="7" timestamp="1283286280">
  <rating>
    <ratings>0</ratings>
    <avg>0.0</avg>
  </rating>
  <files server="834" bucket="1378">
     <image size="515455" content-type="image/jpeg">maidsamaep20scr1.jpg</image>
     <thumb size="8822" content-type="image/jpeg">maidsamaep20scr1.th.jpg</thumb>
  </files>
  <resolution>
    <width>1280</width>
    <height>720</height>
  </resolution>
  <class>r</class>
  <visibility>no</visibility>
  <uploader>
    <ip>69.125.188.189</ip>
  </uploader>
  <links>
    <image_link>http://img834.imageshack.us/img834/1378/maidsamaep20scr1.jpg</image_link>
    <image_html>&lt;a href=&quot;http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img834.imageshack.us/img834/1378/maidsamaep20scr1.jpg&quot; alt=&quot;Free Image Hosting at www.ImageShack.us&quot; border=&quot;0&quot;/&gt;&lt;/a&gt;</image_html>
    <image_bb>[URL=http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg][IMG]http://img834.imageshack.us/img834/1378/maidsamaep20scr1.jpg[/IMG][/URL]</image_bb>
    <image_bb2>[url=http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg][img=http://img834.imageshack.us/img834/1378/maidsamaep20scr1.jpg][/url]</image_bb2>
    <thumb_link>http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg</thumb_link>
    <thumb_html>&lt;a href=&quot;http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg&quot; alt=&quot;Free Image Hosting at www.ImageShack.us&quot; border=&quot;0&quot;/&gt;&lt;/a&gt;</thumb_html>
    <thumb_bb>[URL=http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg][IMG]http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg[/IMG][/URL]</thumb_bb>
    <thumb_bb2>[url=http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg][img=http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg][/url]</thumb_bb2>
    <yfrog_link>http://yfrog.com/n6maidsamaep20scr1j</yfrog_link>
    <yfrog_thumb>http://yfrog.com/n6maidsamaep20scr1j.th.jpg</yfrog_thumb>
    <ad_link>http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg</ad_link>
    <done_page>http://img834.imageshack.us/content.php?page=done&amp;l=img834/1378/maidsamaep20scr1.jpg</done_page>
  </links>

这就是我目前所拥有的。我得到了要上传的文件,并从对NSXMLDocument的响应中获取了数据。唯一的问题是从NSXMLDocument获取image_link。

代码语言:javascript
复制
-(void)openPanelDidEnd:(NSOpenPanel *)openPanel
            returnCode:(int)returnCode
           contextInfo:(void *)x
{
    // Did they chosose "Open"
    if (returnCode == NSOKButton) {
            // Start Upload
        NSLog(@"%@",[openPanel filename]);
        //Set Imageshack Upload API URL
        NSURL *url = [NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        //Set API Key
        [request setPostValue:@"" forKey:@"key"];
        //Set File
        [request setFile:[openPanel filename] forKey:@"fileupload"];
        //Set Progress
        [request setDownloadProgressDelegate:APIProgress];
        [request startSynchronous];
        // Show API Output
        NSString *response = [request responseString];
        NSLog(@"%@",response);
        int httpcode = [request responseStatusCode];
        if (httpcode == 200) {
                    // Perform XML Phasing 
            NSError * error;
            NSArray *itemNodes;
                    // Convert Response Data from NSString to NSData
            NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];
            NSXMLDocument *doc = [[NSXMLDocument alloc] initWithData:data 
                                                             options:0 
                                                               error:&error];
            NSMutableArray* imglinks = [[NSMutableArray alloc] initWithCapacity:13];
            NSLog(@"%@", doc);
                    // XPath to get image_link value only
            itemNodes = [doc nodesForXPath:@"//links/image_link" error:&error];
            for(NSXMLElement* xmlElement in itemNodes)
                [imglinks addObject:[xmlElement stringValue]];
            NSLog(@"%@",[[imglinks objectAtIndex:0]stringValue]);
                    // Insert Image URL to Message
            [fieldmessage setString:[NSString stringWithFormat:@"#image %@ \\n\\n %@",[[imglinks objectAtIndex:0]stringValue],[fieldmessage string]]];
                    // Release unneeded items from memory
            [itemNodes release];
            [doc release];
            [data release];
        }
        else {
            // Upload Failed, show error message
            [self showsheetmessage:@"MelScrobbleX was unable to upload the image you selected" explaination:[NSString stringWithFormat:@"Error: i \\n \\n %@", httpcode, response]];
        }
    }
}

问题是它似乎没有显示来自NSMutableArray的字符串值。它给出了这个错误:

代码语言:javascript
复制
2010-08-31 16:22:13.496 MelScrobbleX[45835:a0f] -[NSCFString stringValue]: unrecognized selector sent to instance 0x12ada80
2010-08-31 16:22:14.026 MelScrobbleX[45835:a0f] -[NSCFString stringValue]: unrecognized selector sent to instance 0x12ada80

从NSMutableArray检索image_link值的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-01 04:42:28

在这条线上

代码语言:javascript
复制
 for(NSXMLElement* xmlElement in itemNodes)
            [imglinks addObject:[xmlElement stringValue]];

在将xmlElement放入数组imglinks之前,您已经获取了它的stringValue。所以,在这一行中

代码语言:javascript
复制
 NSLog(@"%@",[[imglinks objectAtIndex:0]stringValue]);

stringValue是不必要的...你不应该要求一个字符串给出一个字符串值,它已经是一个字符串了!只管去做

代码语言:javascript
复制
 NSLog(@"%@",[imglinks objectAtIndex:0]);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3613005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档