NSXMLParser解析多分区

{

NSString *new;

NSMutableDictionary *dic;

NSMutableArray *arr;

Model *mo;

UITableView *tab;

}



#define MAC @"http://127.0.0.1/LOL.xml"




{    [super viewDidLoad];    //    NSURL *url=[NSURL URLWithString:MAC];    NSURLSession *see=[NSURLSession sharedSession];        NSURLSessionDataTask*tas=[see dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        NSXMLParser *xm=[[NSXMLParser alloc]initWithData:data];        xm.delegate=self;        [xm parse];    }];    [tas resume];            tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];    tab.dataSource=self;    tab.delegate=self;    [self.view addSubview:tab];    }//开始- (void)parserDidStartDocument:(NSXMLParser *)parser;{    //初始化字典    dic=[[NSMutableDictionary alloc]init];        }//结束- (void)parserDidEndDocument:(NSXMLParser *)parser;{    //刷新表格        [tab reloadData];    }- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary*)attributeDict;

{

if ([elementName isEqualToString:@"a"])

{

//准备Key==英雄种类

NSString*str=[attributeDict objectForKey:@"king"];

//准备个数组

arr=[[NSMutableArray alloc]init];

//设置字典key和  值

[dic setObject:arr forKey:str];

}

else if ([elementName isEqualToString:@"fen"])

{

mo=[[Model alloc]init];

[arr addObject:mo];

}

new=elementName;

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;

{

new=nil;

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;

{

if ([new isEqualToString:@"name"])

{

mo.name=string;

}

else if ([new isEqualToString:@"age"])

{

mo.age=string;

}

}

-(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 *str4=@"cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str4];

if (cell==nil)

{

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str4];

}

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] age];

return cell;

}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [dic.allKeys objectAtIndex:section];

}

你可能感兴趣的:(NSXMLParser解析多分区)