views:

248

answers:

3

I am just learning objective-c and iPhone development, and I am really struggling with some very basic tasks. I am only on my 3rd day of the learning process - so that is to be expected somewhat. I'm still almost ashamed to ask such a simple question.

Anyhow, here's my question. I have a .NET web service which I call using a GET for http://somehost/ping

it returns 'pong'

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;pong&lt;/string&gt;

The simplest of test cases.

Back on the iPhone when I retrieve the URL I have the above string as a result. I only want the 'pong' part. This seems like programming 101, but I can't seem to find a simple example of how to do it that doesn't involve defining delagates or other seemingly complex processing steps.

The problem is simple enough, find the first '>' and extract everything from there until the first '<' as an NSString. That's all I need to do.

Does anyone have a basic example of how to do this?

+1  A: 

Hey Sylvanaar, I'm having to do similar types of parsing inside of the client. My general methodolgy for parsing xml responses is like this. I'm pretty sure the classes are available on iphone side too. Note: it may not be the absolute best method, but it does work.

- (id)initWithXMLNode:(NSXMLNode *)node {
    self = [super init];

    if (self != nil) {
     NSError *error;
     NSArray *objects;

     // Get the fingerprint
     objects = [node objectsForXQuery:@"for $fingerprint in ./Fingerprint return data($fingerprint)" error:&error];
     handleErrorInInit(error)

     fingerprint = getFingerprint(objects);

     // Get the moduleName
     objects = [node objectsForXQuery:@"for $moduleName in ./Foldername return data($moduleName)" error:&error];
     handleErrorInInit(error)

     moduleName = getNSString(objects);
    }

    return self;
}

Worth showing this too. Note that NSXMLDocuments are a subclass of NSXMLNodes.

- (NSXMLDocument *)xmlDocumentFromData:(NSData *)data {
    NSError *error; 
    NSXMLDocument *document = [[[NSXMLDocument alloc] initWithData:data options:0 error:&error] autorelease];

    if (error) {
     [NSApp presentError:error];
     return nil;
    } 

    return document; 
}
Bryan McLemore
unfortunately, there's no NSXMLDocument on iPhone.
Ben Gottlieb
Really? How do they expect you to parse xml then?
Bryan McLemore
They supply the event-drives parser NSXMLParser - or you can import libxml2 and use that API.
Kendall Helmstetter Gelner
+1  A: 

Sometimes a full on XML parse makes sense, but a quick index/substring routine can be appropriate as well:

NSRange startBracket = [xmlFragment rangeOfString:@">"];
if(startBracket.location != NSNotFound) {
    NSRange endBracket = [xmlFragment rangeOfString:@"<" 
                                  options:0 
                                  range:NSMakeRange(startBracket.location, 
                                  [xmlFragment length] - startBracket.location)];
    if(endBracket.location != NSNotFound) {
        NSString *value = [[xmlFragment substringFromIndex:startBracket.location+1] 
                            substringToIndex:endBracket.location];
        // Do something with value...
    }
}

(Not tested, needs more error handling, yadda yadda yadda..)

jasondoucette
+3  A: 

This is dry-coded, and kinda ugly imho. But here is a more direct answer.

NSString *xml = @"<tag>pong</tag>";
NSRange range = [xml rangeOfString:@">"];

xml = [xml substringFromIndex:range.location + 1];
range = [substring rangeOfString:@"<"];
xml = [xml substringToIndex:range.location];
Bryan McLemore
This is specifically what I was looking to do. And Hey K!
sylvanaar
Other than having to change the last "rangeOfString:@">" to a @"<", that seems to me the simplest way to parse strings.
nash
fixed, thanks nash.
Bryan McLemore