Skip to content

Instantly share code, notes, and snippets.

@iampaul83
Created April 29, 2015 13:55
Show Gist options
  • Save iampaul83/d68522a59bab9aae5717 to your computer and use it in GitHub Desktop.
Save iampaul83/d68522a59bab9aae5717 to your computer and use it in GitHub Desktop.
kissxml demo
-(void)parseXML:(NSData *) data
{
//文档开始(KissXML和GDataXML一样也是基于DOM的解析方式)
DDXMLDocument *xmlDoc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil];
DDXMLElement *root = [xmlDoc rootElement];
NSString *status = [root elementForName:@"status"].stringValue;
NSString *next_page_token = [root elementForName:@"next_page_token"].stringValue;
NSLog(@"status: %@", status);
NSLog(@"next page: %@",next_page_token);
NSLog(@"==================================================");
//利用XPath来定位节点(XPath是XML语言中的定位语法,类似于数据库中的SQL功能)
NSArray *results = [xmlDoc nodesForXPath:@"PlaceSearchResponse/result" error:nil];
for (DDXMLElement *result in results) {
NSString *name = [result elementForName:@"name"].stringValue;
NSLog(@"%@",name);
NSString *formatted_address = [result elementForName:@"formatted_address"].stringValue;
NSLog(@"%@",formatted_address);
DDXMLElement *geo = [result nodesForXPath:@"geometry/location" error:nil][0];
double lat = [geo elementForName:@"lat"].stringValue.doubleValue;
double lng = [geo elementForName:@"lng"].stringValue.doubleValue;
NSLog(@"(%f,%f)", lat, lng);
NSLog(@"==================================================");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment