新建一个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;
解析XML首先要导入两个库。
导入
SDWebImage开源库
<1>
v
1
.首先
添加
SDWebImage
project
进我们的工程
v
v
2.
添加依赖
v
在
“Build Phases” ->“Link
Binary With
Libraries”
添加
“
ImageIO.framework
”
v
v
3.
添加
Linker
Flag
v
打开
“Build Settings”
-
> “Linking” ->“Other
Linker
Flags”
添加
"
-
ObjC
”
v
v
4.
在使用的地方添加头文件
v
#
import <
SDWebImage
/
UIImageView+WebCache.h
>
<2>
v
再添加
GDataXML
库
v
1
.
选中项目,选中“
Build Setting”
标签页,将顶部的“
basic”
标签页切换到“
all”
v
2.
找到“
search Paths”
的“
header search path”
项,并添加“
/user/include/libxml2"
到列表中。
v
“
/Applications/
Xcode.app
/Contents/Developer/Platforms/
iPhoneOS.platform
/Developer/SDKs/iPhoneOS6.1.sdk/
usr
/include/libxml2
”
v
3.
找到”
Linkingother linker Flags“
项,并添加”
-lxml2“
到列表中。
添加依赖
v4.在“Build Phases” ->“Link Binary With Libraries” 添加
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.