GDataXML
基于libxml2
库,需要做以下配置:
1.导入libxml2库

导入后的结果:

2.设置libxml2库的头文件搜索路径,为了能找到libxml2库的所有头文件
在
Buile Settings
-->Header Search Paths
中加入/usr/include/libxml2

3.设置链接参数,自动链接libxml2库
在Buile Settings
-->Other Linker Flags
中加入-lxml2

4.由于GDataXML是非ARC的,因此得设置编译参数

GDataXML使用:
GDataXML中常用的类
GDataXMLDocument
:代表整个XML文档
GDataXMLElement
:代表文档中的每个元素,使用attributeForName:
方法可以获得属性值
code:
#import "GDataXMLNode.h"
- (void)loadVideos{
// 0.请求路径
NSString *urlString = @"http://www.example.com:8080/videos?type=XML";
NSURL *url = [NSURL URLWithString:urlString];
// 1.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求
__weak typeof(self) weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil) {
// 加载整个文档
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
// 获得所有video元素
NSArray *elements = [doc.rootElement elementsForName:@"video"];
for (GDataXMLElement *ele in elements) {
Video *video = [[Video alloc] init];
video.name = [ele attributeForName:@"name"].stringValue;
video.url = [ele attributeForName:@"url"].stringValue;
video.image = [ele attributeForName:@"image"].stringValue;
video.length = [ele attributeForName:@"length"].stringValue.integerValue;
[weakSelf.videos addObject:video];
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[weakSelf.tableView reloadData];
}];
}
}];
}
网友评论