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;
}
--------------------- 
作者:IOS技术小牛 
来源:CSDN 
原文:https://blog.csdn.net/weixin_42925415/article/details/81584446 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/Py88888/article/details/84136272