我在解析XML的問題,所以我通過我的代碼去與NSLogs,發現 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
不叫。的NSXMLParser「foundCharacters」委託方法
我的XML:
<?xml version="1.0"?>
<locations>
<location>
<name>Americas</name>
<address></address>
<gid>CoQBdAAAAE2tdzw4RGvnvaUZTk6N1ByNtosmD--ZYfcmkkPkR6R6v_nN9gJCO1INOVg-S5rzy7BATEUmvAQzh8hClafZbph2wSgfD28gXNJAttXLUbqzQMaql3rVbisSUv2a2r_H6ktOSaI5lLvf0GNthT5jn2JfAdD_HfUls6qhMvrm5e5XEhDcF5uRjlfpSqI_aciS_QPaGhQVVHenbOKZBQMSoCzsoIsdfw313Q</gid>
<type>
<1>political</1><2/><3/><4/><5/>
</type>
<phone></phone>
<rating></rating>
<icon>http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png</icon>
<website></website>
<longitude>-105.2551187</longitude>
<latitude>54.5259614</latitude>
</location>
</locations>
我實現
- (void)viewDidLoad
{
[super viewDidLoad];
...
if(!self.distance){self.distance = @"250000";}
NSString *string = [NSString stringWithFormat:@"http://fishbe.in/lab/reserverr/api/?term=%@&lat=%f&long=%f&limit=100&distance=%@", self.searchterm, location.coordinate.latitude, location.coordinate.longitude, self.distance];
NSLog(@"%@", string);
NSURL *URL = [NSURL URLWithString:string];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([self.element isEqualToString:@"locations"]) {
self.items = [[NSMutableArray alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSLog(@"HI"); //Not outputted
if ([self.element isEqualToString:@"name"]) {
self.result = [[ReserverrSearchResult alloc] init];
[self.result setTitle:string];
NSLog(@"%@", string);
} else if ([self.element isEqualToString:@"address"]) {
[self.result setVicinity:string];
NSLog(@"%@", string);
} else if ([self.element isEqualToString:@"gid"]) {
[self.result setGid:string];
NSLog(@"%@", string);
} else if ([self.element isEqualToString:@"latitude"]) {
[self.result setLatitude:[string floatValue]];
NSLog(@"%@", string);
} else if ([self.element isEqualToString:@"longitude"]) {
[self.result setLongitude:[string floatValue]];
NSLog(@"%@", string);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"location"]) {
[self.items addObject:self.result];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self addAnnotations];
}
編輯的相關部分 - 增加
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%@",parseError);
}
,並沒有什麼。
添加'parser:parseErrorOccurred:'並查看它是否說了什麼。 – Wain