0

I know that when parsing XML with objective-c most of the time you use NSXMLParser. But what if you only need to read one element. Using NSXMLParser sounds like an overload to me.

The issue is that flickr API doesn't use JSON as response when uploading an image. So my response now is:

<rsp stat="ok">
<photoid>4638598522</photoid>
</rsp>

I only need to know the photoid and I like to know what the best solution will be for this.

Marko Heijnen
  • 56
  • 2
  • 8

3 Answers3

4

Here are the two lines of code to get the photoid:

NSString *source = @"<rsp stat=\"ok\"><photoid>4638598522</photoid></rsp>";
NSLog(@"photoid: %@", [[[[source componentsSeparatedByString:@"<photoid>"] objectAtIndex:1] componentsSeparatedByString:@"</photoid>"] objectAtIndex:0]);
Hoang Pham
  • 6,899
  • 11
  • 57
  • 70
0

Treat the xml as a string and use a regular expression to extract the element you want. Perhaps with the RegexKitLite library. You can create the regular expression with help from gskinner's online regex tool.

possibly just with this :

([0-9])
Ross
  • 14,266
  • 12
  • 60
  • 91
0
int main(int argc, char* argv[]){
    int num;
    if (argc < 2) return -1;
    printf("arg: %s", argv[1]);
    sscanf(argv[1], "id>%d</", &num);
    printf("result: %i", num);
    if (result < 0) return -1;
    return 0;
}

Simply use sscanf. It should do the trick just fine. (I think NSString has a similar method, if not you can grab the c char array from it then pass it back).

CodeJoust
  • 3,760
  • 21
  • 23
  • 1
    I was searching a little bit more on stackoverflow and found this link: http://stackoverflow.com/questions/594797/how-to-use-nsscanner. For me that does do the trick. thanks for putting me in the right direction – Marko Heijnen May 25 '10 at 11:23