xPath与命名空间与动态加载图片

读取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]);

    }

}

将默认的天气图添加到工程来。
在xib中将默认图片显示出来 。
关联成员变量 _imgView
将图片开源代码放到工程中 https://github.com/rs/SDWebImage

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
ImageIo.framework
MapKit.framework
源码地址:http://download.csdn.net/detail/cloud95/5192648

你可能感兴趣的:(xPath与命名空间与动态加载图片)