There are a couple of issues:
As wain points out, you should implement
parser:parseErrorOccurred:
and it will tell you what's wrong.I'd expect problems if the
searchterm
contains any characters that are reserved characters for a URL (e.g. a space, a plus sign, an ampersand, etc.). You should always percent-escape what I presume is a user provided search term and adding it to a URL:NSString *string = [NSString stringWithFormat:@"http://fishbe.in/lab/reserverr/api/?term=%@&lat=%f&long=%f&limit=100&distance=%@", [self percentEscapeString:self.searchterm], location.coordinate.latitude, location.coordinate.longitude, self.distance];
where you can use a method like the following:
- (NSString *)percentEscapeString:(NSString *)string { NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, (CFStringRef)@" ", (CFStringRef)@":/?@!$&'()*+,;=", kCFStringEncodingUTF8)); return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; }
Note, do not rely upon
stringByAddingPercentEscapesUsingEncoding
to do the percent-escaping. You really want to useCFURLCreateStringByAddingPercentEscapes
, like above.As an aside, your XML is not well-formed. I don't believe that
NSXMLParser
will accept element names that are numbers. So, instead of:<type> <1>political</1><2/><3/><4/><5/> </type>
You might want:
<types> <type>political</type> <type></type> <type></type> <type></type> <type></type> </types>
Or, if you really need those numeric identifiers:
<types> <type id="1">political</type> <type id="2"></type> <type id="3"></type> <type id="4"></type> <type id="5"></type> </types>
In this latter example, your
didStartElement
can then use theattributeDict
to extract theid
identifier.You report that
parseErrorOccurred
did not return anything. That means your server probably didn't return anything. And this could be caused by any of a number of problems. Try using your URL and return the data (for example, into aNSString
orNSData
, rather than the parser) and see what the response looked like. E.g., for testing purposes you could do:NSError *error; NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error]; NSString *results = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // confirm error is `nil` and `results` is well-formed XML; // you can then pass the `data` to `NSXMLParser` with `initWithData` if you want
I bet it's empty (which for certain search terms could be caused by a lack of percent-escaping, or from any of a myriad of server problems).
It might not be a problem, but it's worth noting that it's conceptually risky to save the results in
foundCharacters
, because sometimes betweendidStartElement
anddidEndElement
, you'll have multiple calls tofoundCharacters
to retrieve the data. Generally people will instantiate aNSMutableString
indidStartElement
, append to it infoundCharacters
, and then save it indidEndElement
.