GDataXML
基于libxml2
庫(kù)靠抑,需要做以下配置:
1.導(dǎo)入libxml2庫(kù)
12-導(dǎo)入libxml2.tbd庫(kù).jpg
導(dǎo)入后的結(jié)果:
13-導(dǎo)入libxml2.tbd庫(kù).jpg
2.設(shè)置libxml2庫(kù)的頭文件搜索路徑,為了能找到libxml2庫(kù)的所有頭文件
在
Buile Settings
-->Header Search Paths
中加入/usr/include/libxml2
14-導(dǎo)入libxml2庫(kù)頭文件搜索路徑.jpg
3.設(shè)置鏈接參數(shù),自動(dòng)鏈接libxml2庫(kù)
在Buile Settings
-->Other Linker Flags
中加入-lxml2
15-自動(dòng)鏈接libxml2庫(kù).jpg
4.由于GDataXML是非ARC的笋熬,因此得設(shè)置編譯參數(shù)
16-設(shè)置該文件為MRC模式.jpg
GDataXML使用:
GDataXML中常用的類
GDataXMLDocument
:代表整個(gè)XML文檔
GDataXMLElement
:代表文檔中的每個(gè)元素,使用attributeForName:
方法可以獲得屬性值
code:
#import "GDataXMLNode.h"
- (void)loadVideos{
// 0.請(qǐng)求路徑
NSString *urlString = @"http://www.example.com:8080/videos?type=XML";
NSURL *url = [NSURL URLWithString:urlString];
// 1.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.發(fā)送請(qǐng)求
__weak typeof(self) weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil) {
// 加載整個(gè)文檔
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];
}];
}
}];
}