网络:XML 解析(使用GDataXMLNode第三方框架)

#import "ViewController.h"
#import "CZVideo.h"
//#import "CZParseVideoXml.h"
#import "CZVideoCell.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSArray *data; // 数组数据
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupRefresh];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZVideoCell" forIndexPath:indexPath];

    // 设置行对应的数据
    cell.video = self.data[indexPath.row];

    return cell;
}


// 初始化刷新控件
- (void)setupRefresh {
    self.refreshControl = [[UIRefreshControl alloc]init];

    // 添加事件
    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];


    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:@"正在玩命加载中..." attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]}];

    // 设置标题
    self.refreshControl.attributedTitle = attributeStr;
//    self.refreshControl.tintColor = [UIColor redColor];
}

// xml 解析都是一个模型一个解析文件
- (IBAction)loadData {
    // NSURL
    NSURL *url = [NSURL URLWithString:@"http://localhost/videos.xml"];
    // 创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:15];

    // 异步发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

        GDataXMLDocument *xml = [[GDataXMLDocument alloc]initWithData:data error:nil];

        NSMutableArray *dataM = [NSMutableArray array];
        // 根节点
        GDataXMLElement *root = xml.rootElement;
        [root.children enumerateObjectsUsingBlock:^(GDataXMLElement *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 创建模型
            CZVideo *video = [[CZVideo alloc]init];
//            NSLog(@"%@",obj.attributes);
            video.videoId = @([[[obj.attributes lastObject] stringValue] integerValue]);
            [dataM addObject:video];
            // video
            [obj.children enumerateObjectsUsingBlock:^(GDataXMLElement *  _Nonnull objChildren, NSUInteger idx, BOOL * _Nonnull stop) {
                // name/length/...
//                NSLog(@"%@",objChildren.stringValue);
                NSString *key = objChildren.name;
                NSString *value = objChildren.stringValue;
                [video setValue:value forKey:key];

            }];
        }];
        self.data = dataM.copy;
        [self.tableView reloadData];
        NSLog(@"%@",self.data);

    }];

}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值