1

我正在尝试使用 iPhone SDK 中的 NSXMLParser 解析来自 Geonames 的调用。我以前用过这个,但由于某种原因,我得到了一个空字典,即使我在网络浏览器中得到了结果。有人可以指出我可能做错了什么。

以下是我正在使用的代码以及将其粘贴到浏览器中的结果。

提前致谢

 #pragma mark -
#pragma mark - Parcer Services

-(void)beginLoadingFeed{
 //[self startLoadingWithMessage:@"Loading Results...."]; 
 NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadFeed) object:nil];
 [operationQueue addOperation:operation];
 [operation release];
}

- (void)loadFeed{
 NSString *path = [NSString stringWithFormat:@"http://ws.geonames.org/postalCodeSearch?placename=%@&long&maxRows=20",self.location];
 [Logger log:@"Geonames Query: %@",path];
 [self parseXMLFileAtURL:path]; 
 [self performSelectorOnMainThread:@selector(didfinishedLoadingFeed) withObject:nil waitUntilDone:YES];
}

-(void)didfinishedLoadingFeed{

}

- (void)parserDidStartDocument:(NSXMLParser *)parser{ 
 [Logger log:@"found file and started parsing"]; 
}


//Called when the parser runs into an open tag (<tag>)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

 if ([elementName isEqualToString:@"code"]) {
  currentResult = [NSMutableDictionary dictionary]; 
 } else {
  currentElement = [elementName copy];
 }

}

//This is just to resolve random HTML entities
- (NSData *)parser:(NSXMLParser *)parser resolveExternalEntityName:(NSString *)entityName systemID:(NSString *)systemID {
 return [entityName dataUsingEncoding:NSASCIIStringEncoding];
} 


- (void)parseXMLFileAtURL:(NSString *)URL{ 
 self.results = [[[NSMutableArray alloc] init] autorelease];
 NSURL *xmlURL = [NSURL URLWithString:URL];
 NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
 [parser setDelegate:self];
 [parser parse];
 [parser autorelease];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
 NSString * errorString = [NSString stringWithFormat:@"Unable to connect to web site (Error code %i )", [parseError code]];
 [Logger log:@"error parsing : %@", errorString]; 
 [self stopLoadingView];
 [self showMessage:@"Error loading content" withTitle:@"Error Loading"];
}



/*** Called when the parser runs into a close tag (</tag>). If it is the Result tag that is closing, we should add the currentResult to the array, and then forget about it ***/
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
 if ([elementName isEqualToString:@"code"]) {
   [self.results addObject:currentResult];
   currentResult = nil;
 }
}


- (void)parserDidEndDocument:(NSXMLParser *)parser {
 [Logger log:@"all done!"]; 
 [Logger log:@"results array has %d items", [self.results count]];
 [Logger log:@"Results:%@",results];
 [theTableView reloadData];
 [self stopLoadingView];
}

以下是在搜索“波士顿”一词时使用上述相同调用在浏览器中返回的结果:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<geonames>
<totalResultsCount>2808</totalResultsCount>
<code>
<postalcode>02101</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.370567</lat>
<lng>-71.026964</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02108</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.357603</lat>
<lng>-71.068432</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02109</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.360027</lat>
<lng>-71.054495</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02110</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.357636</lat>
<lng>-71.051417</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02111</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.350348</lat>
<lng>-71.0629</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02114</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.361111</lat>
<lng>-71.06823</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02115</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.342706</lat>
<lng>-71.092215</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02116</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.349201</lat>
<lng>-71.076798</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02118</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.336162</lat>
<lng>-71.072854</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02128</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.364197</lat>
<lng>-71.025694</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02199</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.347873</lat>
<lng>-71.082543</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02210</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.348921</lat>
<lng>-71.046511</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02215</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.347088</lat>
<lng>-71.102689</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>22713</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>38.538241</lat>
<lng>-78.142285</lng>
<adminCode1>VA</adminCode1>
<adminName1>Virginia</adminName1>
<adminCode2>047</adminCode2>
<adminName2>Culpeper</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>24592</postalcode>
<name>South Boston</name>
<countryCode>US</countryCode>
<lat>36.696335</lat>
<lng>-78.918829</lng>
<adminCode1>VA</adminCode1>
<adminName1>Virginia</adminName1>
<adminCode2>083</adminCode2>
<adminName2>Halifax</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02102</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.338947</lat>
<lng>-70.919635</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02103</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.338947</lat>
<lng>-70.919635</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02104</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.338947</lat>
<lng>-70.919635</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02105</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.338947</lat>
<lng>-70.919635</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
<code>
<postalcode>02106</postalcode>
<name>Boston</name>
<countryCode>US</countryCode>
<lat>42.354318</lat>
<lng>-71.073449</lng>
<adminCode1>MA</adminCode1>
<adminName1>Massachusetts</adminName1>
<adminCode2>025</adminCode2>
<adminName2>Suffolk</adminName2>
<adminCode3/>
<adminName3/>
</code>
</geonames>
4

1 回答 1

1

你得到一个空字典,因为currentResult从来没有给出任何数据。didStartElementanddidEndElement方法实际上并没有给你<foo>bar</foo>标签内的内容。

要获取元素的实际内容,请使用- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string方法。

于 2010-06-04T06:34:59.587 回答