//把您的網址在這裏
使用NSXML解析器首先需要聲明的NSXMLParser代表在.H。然後嘗試此代碼
NSString * urlString = [NSString stringWithFormat:@"http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=%@",CountryName];
NSLog(@"%@",urlString);
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData * data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"%@",data);
xmlResultString = [[NSMutableString alloc]init];
NSLog(@"%@",xmlResultString);
parser = [[NSXMLParser alloc]initWithData:data];
[parser setDelegate:self];
[parser parse];
// return result;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
xmlDictionary=[[NSDictionary alloc]init];
[xmlResultString appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSLog(@"%@",xmlResultString);
xmlDictionary = [XMLReader dictionaryForXMLString:xmlResultString error:nil];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"%@",xmlDictionary);
NSArray * City=[[NSArray alloc]init];
NSString* city =[[[xmlDictionary valueForKey:@"NewDataSet"] valueForKey:@"Table"] valueForKey:@"City"];
NSLog(@"%@",city);
City=[[[xmlDictionary valueForKey:@"NewDataSet"] valueForKey:@"Table"] valueForKey:@"City"];
NSLog(@"%@",City);
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
result = 0;
NSLog(@"%@",parseError);
}
NSString * urlString = [NSString stringWithFormat:@"http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=%@",CountryName];
NSLog(@"%@",urlString);
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData * data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"%@",data);
xmlResultString = [[NSMutableString alloc]init];
NSLog(@"%@",xmlResultString);
parser = [[NSXMLParser alloc]initWithData:data];
[parser setDelegate:self];
[parser parse];
// return result;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
xmlDictionary=[[NSDictionary alloc]init];
[xmlResultString appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSLog(@"%@",xmlResultString);
xmlDictionary = [XMLReader dictionaryForXMLString:xmlResultString error:nil];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"%@",xmlDictionary);
NSArray * City=[[NSArray alloc]init];
NSString* city =[[[xmlDictionary valueForKey:@"NewDataSet"] valueForKey:@"Table"] valueForKey:@"City"];
NSLog(@"%@",city);
City=[[[xmlDictionary valueForKey:@"NewDataSet"] valueForKey:@"Table"] valueForKey:@"City"];
NSLog(@"%@",City);
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
result = 0;
NSLog(@"%@",parseError);
}
感謝您的迴應,但它是我們的要求,使用TBXML解析和SOAP消息的請求...所以你可以請給我解決方案 –