2010-06-03 58 views
1

我想解析從iPhone SDK中的NSXMLParser與Geonames的調用。我之前使用過這個,但由於某種原因,即使我在Web瀏覽器中返回結果,我仍然收到空字典。有人可以指出我可能做錯了什麼。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> 

回答

1

你得到一個空字典,因爲currentResult永遠不會被提供任何數據。 didStartElementdidEndElement方法實際上並不會給您提供<foo>bar</foo>標籤中的內容。

要獲取元素的實際內容,請使用- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string方法。