Oc GDataXML(DOM)解析表格

首先导入第三方GDataXML

手写xml文件




    
            赏金猎人
            枪林弹雨
    
    
            提莫
            老司机
    
    
            流浪
            禁锢
    


Model类
Hero.h

#import 

@interface Hero : NSObject
@property (nonatomic,strong)NSString *name,*like;
@end

控制器类
ViewController.m

#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Hero.h"
@interface ViewController ()
{
    NSMutableArray *_arr;
    UITableView *_table;
}

@end
#define TEST_URL @"http://127.0.0.1/test.xml"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL URLWithString:TEST_URL];
    
   NSURLSession *session =  [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        //DOM解析
        GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
        //获得跟标签的内容
        GDataXMLElement *root = [doc rootElement];
        //初始化数组
        _arr = [[NSMutableArray alloc]init];
        //遍历root标签下的所有hero标签对
        for (GDataXMLElement *heroEle in [root elementsForName:@"hero"])
        {
            Hero *hero  = [[Hero alloc]init];
            //获得hero标签的name标签
            NSArray *nameArr = [heroEle elementsForName:@"name"];
            //获得唯一一个name标签
            GDataXMLElement *nameEle = [nameArr firstObject];
            //去掉标签获得内容
            NSString *str = [nameEle stringValue];
            
            hero.name = str;
            
            
            hero.like = [[[heroEle elementsForName:@"like"]firstObject ]stringValue];
            //将对象添加到数组里
            [_arr addObject:hero];
        }
        NSLog(@"%@",_arr);
        //刷新表格
        dispatch_async(dispatch_get_main_queue(), ^{
            [_table reloadData];
        });
    }];
    
    [task resume];
    
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
    }
    cell.textLabel.text = [[_arr objectAtIndex:indexPath.row]name];
    cell.detailTextLabel.text = [[_arr objectAtIndex:indexPath.row]like];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

你可能感兴趣的:(Oc GDataXML(DOM)解析表格)