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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值