新建一个Singal View Application 的工程,并在.xib文件中拖入如下控件
。
为每个控件都关联一个outlet,
@property (retain, nonatomic) IBOutlet UILabel *_todayInfo;
@property (retain, nonatomic) IBOutlet UILabel *_todayTemp;
@property (retain, nonatomic) IBOutlet UIImageView *_imageviewtoday;
@property (retain, nonatomic) IBOutlet UILabel *_nextInfo;
@property (retain, nonatomic) IBOutlet UILabel *_nextTemp;
@property (retain, nonatomic) IBOutlet UIImageView *_imageviewtomorrow;
导入SDWebImage开源库
<1>
v1.首先添加SDWebImageproject进我们的工程
v
v2.添加依赖
v在“Build Phases” ->“LinkBinary WithLibraries”添加“ImageIO.framework”
v
v3.添加LinkerFlag
v打开“Build Settings”-> “Linking” ->“OtherLinkerFlags”添加"-ObjC”
v
v4. 在使用的地方添加头文件
v#import <SDWebImage/UIImageView+WebCache.h>
<2>
v再添加GDataXML库
v1.选中项目,选中“BuildSetting”标签页,将顶部的“basic”标签页切换到“all”
v2.找到“searchPaths”的“headersearchpath”项,并添加“/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/
Developer/SDKs/iPhoneOS6.1.sdk/usr/include/libxml2”
到列表中。
v3.找到”Linking\otherlinkerFlags“项,并添加”-lxml2“到列表中。
v4.在“Build Phases” ->“LinkBinary WithLibraries”添加
frameworks:libxml2.2.dylb
接着在ViewController.m文件中引入两个库的头文件
#import <SDWebImage/UIButton+WebCache.h>
#import "GDataXMLNode.h"
最后在- (void)viewDidLoad{ }函数中加入如下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// 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=[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil];
GDataXMLDocument *doc = [[[GDataXMLDocument alloc]initWithXMLString:xml options:0 error:nil]autorelease];
NSArray *nodes=[doc.rootElement children];
for(GDataXMLNode *node in nodes){
NSLog(@"node=%@",[node stringValue]);
}
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];
GDataXMLNode *n5 = [nodes objectAtIndex:9];
GDataXMLNode *n6 = [nodes objectAtIndex:16];
NSString *strImage=[NSString stringWithFormat:@"http://www.webxml.com.cn/images/weather/b_%@",[n5 stringValue]];
[_imageviewtoday setImageWithURL:[NSURL URLWithString:strImage] placeholderImage:[UIImage imageNamed:@"b_nothing.gif"]];
strImage=[NSString stringWithFormat:@"http://www.webxml.com.cn/images/weather/b_%@",[n6 stringValue]];
[_imageviewtomorrow setImageWithURL:[NSURL URLWithString:strImage] placeholderImage:[UIImage imageNamed:@"b_nothing.gif"]];
}
运行结果如下,已经正确的从网上读取到xml文件并解析成功。
以上链接的XML文件如下,可以对照着XML文件和代码,看看是如何解析xml的。
This XML file does not appear to have any style information associated with it. The document tree is shown below.