1、GDataXML配置
- 在Other Linker Flags中加入-lxml2
- 在Head Search Path中加入/usr/include/libxml2
2、GDataXML的具体用法
// 加载整个文档
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
// 获得根节点
doc.rootElement;
// 获得其他节点
[element elementsForName:@"video"];
// 获得节点的属性
[element attributeForName:@"name"].stringValue;
3、GDataXML解析小案例
#import "ViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
#import "XMGVideo.h"
#import <MJExtension/MJExtension.h>
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *videos; /**< 视屏信息 */
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 只需要在字典转模型之前, 告诉框架要将模型中的哪个属性和字典中的哪个KEY对应
[XMGVideo setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"ID":@"id"};
}];
self.tableView.rowHeight = 150;
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 1.创建解析器
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:kNilOptions error:nil];
// 2.获取根元素
GDataXMLElement *rootElement = document.rootElement;
// 3.从根元素中获取所有子元素
NSArray *elements = [rootElement elementsForName:@"video"];
// 4.遍历子元素, 取出属性转换为模型
for (GDataXMLElement *element in elements) {
XMGVideo *video = [[XMGVideo alloc] init];
video.ID = @([element attributeForName:@"id"].stringValue.integerValue);
video.image = [element attributeForName:@"image"].stringValue;
video.url = [element attributeForName:@"url"].stringValue;
video.name = [element attributeForName:@"name"].stringValue;
video.length = @([element attributeForName:@"length"].stringValue.integerValue);
[self.videos addObject:video];
}
// 5.刷新表格
[self.tableView reloadData];
}];
}
#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.videos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// 2.取出对应行的字典
XMGVideo *video = self.videos[indexPath.row];
// 2.1设置数据
cell.textLabel.text = video.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%@", video.length];
NSString *urlStr = [NSString stringWithFormat:@"http://120.25.226.186:32812/%@", video.image];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil];
// 3.返回cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.取出选中行对应的字典
XMGVideo *video = self.videos[indexPath.row];
// 2.根据字典中的URL属性拼接视屏的地址
NSString *urlStr = [NSString stringWithFormat:@"http://120.25.226.186:32812/%@", video.url];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// 3.播放视屏
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
// 4.显示控制器
[self presentViewController:vc animated:YES completion:nil];
}
#pragma mark - lazy
- (NSMutableArray *)videos
{
if (!_videos) {
_videos = [NSMutableArray array];
}
return _videos;
}
@end