Oc NSXMLParse(SAX)解析分区表格

手写xml文件



    
        
            赏金猎人
            枪林弹雨
        
        
            寒冰射手
            万箭齐发
        
        
            皮城女警
            让子弹飞
        
    


    
        流浪法师
        禁锢
    
    
        提莫
        种蘑菇
    




    
        德玛西亚
        三爪
    
    
        雷霆咆哮
        枪林弹雨
    
    
        皮夹龙骨
        无线
    





Model类
Hero.h

#import 

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

控制器类
ViewController.m

#import "ViewController.h"
#import "Hero.h"
@interface ViewController ()
{
    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 *)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

你可能感兴趣的:(Oc NSXMLParse(SAX)解析分区表格)