tags:

views:

51

answers:

1

I'm using an API to upload an image and I need to get the uploaded image URL (image_link) from the XML output shown below

<?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&lt;/image_link&gt;
    <image_html>&lt;a href=&quot;http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg&amp;quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img834.imageshack.us/img834/1378/maidsamaep20scr1.jpg&amp;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]&lt;/image_bb&gt;
    <image_bb2>[url=http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg][img=http://img834.imageshack.us/img834/1378/maidsamaep20scr1.jpg][/url]&lt;/image_bb2&gt;
    <thumb_link>http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg&lt;/thumb_link&gt;
    <thumb_html>&lt;a href=&quot;http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg&amp;quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg&amp;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]&lt;/thumb_bb&gt;
    <thumb_bb2>[url=http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg][img=http://img834.imageshack.us/img834/1378/maidsamaep20scr1.th.jpg][/url]&lt;/thumb_bb2&gt;
    <yfrog_link>http://yfrog.com/n6maidsamaep20scr1j&lt;/yfrog_link&gt;
    <yfrog_thumb>http://yfrog.com/n6maidsamaep20scr1j.th.jpg&lt;/yfrog_thumb&gt;
    <ad_link>http://img834.imageshack.us/my.php?image=maidsamaep20scr1.jpg&lt;/ad_link&gt;
    <done_page>http://img834.imageshack.us/content.php?page=done&amp;amp;l=img834/1378/maidsamaep20scr1.jpg&lt;/done_page&gt;
  </links>

This is what I currently have. I got the file to upload and I took the data from the response to an NSXMLDocument. The only problem is getting the image_link from the NSXMLDocument.

-(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]];
        }
    }
}

The problem is that it doesn't seem to show the string value from the NSMutableArray. It gives me this error:

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

What is the correct way in retrieving the image_link value from NSMutableArray?

+2  A: 

In this line

 for(NSXMLElement* xmlElement in itemNodes)
            [imglinks addObject:[xmlElement stringValue]];

You already took stringValue of the xmlElement before putting it into the array imglinks. So, in this line

 NSLog(@"%@",[[imglinks objectAtIndex:0]stringValue]);

stringValue is unnecessary... you shouldn't ask a string to give a string value, it's already a string! Just do

 NSLog(@"%@",[imglinks objectAtIndex:0]);
Yuji
It did remove that error, but still does not report image_link back. It gives me an out of bounds error for index 0 of 0
chikorita157
chikorita: That's because the array is empty (no objects in it—the second 0). You didn't receive any `image_link` elements on that run. Either the data changed, or you changed the code in some other way and so broke the extraction of `image_link` elements' string values.
Peter Hosey
My bad... I messed up the XPath trying to get the thing working. Since I reverted it back to what I posted and it works.
chikorita157