Oc NSXMLParse(SAX)解析分区表格

手写xml文件

<?xml version="1.0" encoding="utf-8"?>
<root>
    <sort kind = "ADC">
        <hero>
            <name>赏金猎人</name>
            <like>枪林弹雨</like>
        </hero>
        <hero>
            <name>寒冰射手</name>
            <like>万箭齐发</like>
        </hero>
        <hero>
            <name>皮城女警</name>
            <like>让子弹飞</like>
        </hero>
    </sort>

<sort kind = "AP">
    <hero>
        <name>流浪法师</name>
        <like>禁锢</like>
    </hero>
    <hero>
        <name>提莫</name>
        <like>种蘑菇</like>
    </hero>

</sort>

<sort kind = "T">
    <hero>
        <name>德玛西亚</name>
        <like>三爪</like>
    </hero>
    <hero>
        <name>雷霆咆哮</name>
        <like>枪林弹雨</like>
    </hero>
    <hero>
        <name>皮夹龙骨</name>
        <like>无线</like>
    </hero>
</sort>


</root>

Model类
Hero.h

#import <Foundation/Foundation.h>

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

控制器类
ViewController.m

#import "ViewController.h"
#import "Hero.h"
@interface ViewController ()<NSXMLParserDelegate,UITableViewDelegate,UITableViewDataSource>
{
    NSMutableDictionary *_dic;
    NSMutableArray *_arr;
    NSString *_newElementName;
    Hero *_hero;
    UITableView *_table;
}
@end
#define TEST_URL @"http://127.0.0.1/textSort.xml"
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

#if 0
    NSURL *url = [NSURL URLWithString:TEST_URL];

    NSURLRequest *requset = [NSURLRequest requestWithURL:url];

    NSData *data = [NSURLConnection sendSynchronousRequest:requset returningResponse:nil error:nil];

    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];

    parser.delegate = self;

    [parser parse];

#elif 1

    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) {

        NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];

        parser.delegate = self;

        [parser parse];


    }];

    //开始请求
    [task resume];
    [_table reloadData];

#endif
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];

    _table.delegate =self;
    _table .dataSource = self;

    [self.view addSubview:_table];
}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    _dic = [[NSMutableDictionary alloc]init];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"sort"])
    {
        //给字典设置key和值 以英雄类型为key,
        NSString *key = [attributeDict objectForKey:@"kind"];
        _arr  = [[NSMutableArray alloc]init];
        //给字典设置数值
        [_dic setObject:_arr forKey:key];
    }
    else if ([elementName isEqualToString:@"hero"])
    {
        _hero = [[Hero alloc]init];

        [_arr addObject:_hero];
    }

    _newElementName = elementName;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([_newElementName isEqualToString:@"name"])
    {
        _hero.name = string;
    }
    else if ([_newElementName isEqualToString:@"lile"])
    {
        _hero.like = string;
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    _newElementName = nil;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{

}

//分区
- (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];
}

- (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];
    }
    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];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值