读取xml还可以根据路径指定。
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *url=[@"http://webservice.webxml.com.cn/Webservices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=北京"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *xml=[NSStringstringWithContentsOfURL:[NSURLURLWithString:url] encoding:NSUTF8StringEncodingerror:nil];
GDataXMLDocument *doc=[[[GDataXMLDocumentalloc] initWithXMLString:xmloptions:0error:nil]autorelease];
// NSArray *nodes=[doc.rootElement children];
//
// GDataXMLNode *n1=[nodes objectAtIndex:6];
// _todayInfo.text=[n1 stringValue];
// GDataXMLNode *n2=[nodes objectAtIndex:5];
// _todayTemp.text=[n2 stringValue];
// GDataXMLNode *n3=[nodes objectAtIndex:13];
// _nextInfo.text=[n3 stringValue];
// GDataXMLNode *n4=[nodes objectAtIndex:12];
// _nextTemp.text=[n4 stringValue];
NSError *err=nil;
NSDictionary *namespace=[NSDictionarydictionaryWithObject:@"http://WebXml.com.cn/"forKey:@"cc"];
NSArray *ns=[docnodesForXPath:@"//cc:ArrayOfString//cc:string[11]"namespaces:namespaceerror:&err];
if (err) {
NSLog(@"err=%@",[errlocalizedDescription]);
}
else
{
NSLog(@"count=%d",[nscount]);
GDataXMLNode *n=[ns lastObject];
NSLog(@"%@",[nstringValue]);
}
}
NSArray *img=[doc nodesForXPath:@"//cc:ArrayOfString//cc:string[9]"namespaces:namespaceerror:&err];
GDataXMLNode *nn=[img lastObject];
NSString *strImage=[NSStringstringWithFormat:@"http://www.webxml.com.cn/images/weather/b_%@",[nnstringValue]];
[_imgViewsetImageWithURL:[NSURLURLWithString:strImage]placeholderImage:[UIImageimageNamed:@"b_nothing.gif"]];
加入FrameWork