DOM解析XMl文档

一、编写 xml 文件
XML文件
将写好的XML文档 放入到 桌面 –> 【Documents的替身】中
在网络上验证XML文档是否书写正确:网址:http://127.0.0.1/文档名

二、创建继承于 NSObject 的类,例:类名为 Luck
在Luck.h 文件中定义属性【属性名与XML文件中的属性名相同】

@property (nonatomic ,strong) NSString *name, *like;

三、在 ViewController.h 中创建表格
签表格协议

<UITableViewDelegate , UITableViewDataSource>
// 定义成员变量 -- 表格/可变字典
{
    NSMutableDictionary *_dic;

    UITableView *_table;
}
// 初始化表格
_table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
// 定义宏 -- XML文件的链接
#define TEST_URL @"http://127.0.0.1/XML文档名"
// DOM解析 -- 导入第三方文档
// 创建连接对象
NSURL *url = [NSURL URLWithString:TEST_URL];

// 初始化 session
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 *rootEle = [doc rootElement];

    // 初始化字典
    _dic = [[NSMutableDictionary alloc] init];

    // 遍历
    for (GDataXMLElement *luckEle in [rootEle elementsForName:@"luck"]) {

        // 创建字符串 获取字典的key
        NSString *key = [[luckEle attributeForName:@"kind"] stringValue];

        // 创建数组
        NSMutableArray *myArr = [[NSMutableArray alloc] init];

        // 遍历 root 标签中的 luck 标签
        for (GDataXMLElement *heroEle in [luckEle elementsForName:@"people"]) {

            // 实始化对象
            Luck *myLuck = [[Luck alloc] init];

            // 创建数组 获取 luck 标签下的 name 标签
            NSArray *nameArr = [heroEle elementsForName:@"name"];

            // 获取数组中唯一的值
            GDataXMLElement *nameEle = [nameArr firstObject];

            // 获取标签中的内容
            myLuck.name = [nameEle stringValue];

            // 获取 like 标签内容
            myLuck.like = [[[heroEle elementsForName:@"like"] firstObject] stringValue];
            // 添加到数组
            [myArr addObject:myLuck];
        }

        // 设置字典
        [_dic setObject:myArr forKey:key];
    }

    // 返回主线程 刷新表格
    dispatch_async(dispatch_get_main_queue(), ^{

       // 刷新表格
        [_table reloadData];
    });

}];

// 开始请求
[task resume];

设置表格分区数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dic.count;
}

设置表格行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [_dic.allKeys objectAtIndex:section];

    return [[_dic objectForKey:key] count];
}

设置cell内容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
    }

    NSString *key = [_dic.allKeys objectAtIndex:indexPath.section];
    cell.textLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] like];

    return cell;
}
// 页眉 -- 分区名
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_dic.allKeys objectAtIndex:section];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值