GDataXML解析XML分组

    //导航条右侧按钮的触发方法
- (void)jiexi{
    //地址
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/StuClass.xml"];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    GDataXMLElement *rootelement = [doc rootElement];
    //class组头
    NSArray *Class1 = [rootelement elementsForName:@"class"];
    for (GDataXMLElement *calssElement in Class1) {
        //获取组头的title
        NSString *ClassNameStr = [[calssElement attributeForName:@"a_name"] stringValue];
        //把组头title添加到数组
        [ClassNameArr addObject:ClassNameStr];

        NSArray *stu = [calssElement elementsForName:@"student"];

        NSMutableArray *arr1 = [NSMutableArray new];

        for (GDataXMLElement *stuElement in stu) {

            Student *s = [Student new];

            s.name = [[[stuElement elementsForName:@"name"] firstObject] stringValue];
            s.age = [[[stuElement elementsForName:@"age"] firstObject] stringValue];
            s.sex = [[[stuElement elementsForName:@"sex"] firstObject] stringValue];
            //把数据添加到名称数组
            [arr1 addObject:s];
        }
        //把名称数组添加到  总的数组里
        [array addObject:arr1];

    }
    //刷新表格
    [tbv reloadData];
}
    //表格表头的title方法
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return ClassNameArr[section];
}
//表格
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [array[section]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *oj = @"34";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:oj];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:oj];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@",[array[indexPath.section][indexPath.row]name] , [array[indexPath.section][indexPath.row]age] , [array[indexPath.section][indexPath.row]sex]];
    return cell;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值