#import “Model.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,NSXMLParserDelegate>
@property(nonatomic ,strong)UITableView *ojtable;
@property(nonatomic , strong)NSMutableArray *arr;// 最后的数据源
@property(nonatomic , strong)NSMutableArray *classArr;// 班级数组
@property(nonatomic , strong)NSMutableArray *GroupArr;// 分区的学生
@property(nonatomic , strong)Model *model;// 每次获取到的学生
@property(nonatomic , strong)NSString *str;// 每次获取到的内容
@end
@implementation ViewController
-(UITableView *)ojtable{
if(!_ojtable){
_ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_ojtable.delegate = self;
_ojtable.dataSource = self;
}
return _ojtable;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @“SAX解析”;
NSURL *url = [NSURL URLWithString:@“http://127.0.0.1/abc.xml”];
NSXMLParser *xml = [[NSXMLParser alloc]initWithContentsOfURL:url];
xml.delegate = self;
[xml parse];
[self.view addSubview:self.ojtable];
}
//开始解析 - (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@“开始解析文档”);
self.arr = [[NSMutableArray alloc]init];
self.classArr = [[NSMutableArray alloc]init];
}
//结束解析
-
(void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@“已经结束解析文档”);
[self.ojtable reloadData];
}
//开始标签
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
NSLog(@“遇到开始标签:%@”,elementName);
if([elementName isEqualToString:@“student”]){
self.model = [[Model alloc]init];
}
if([elementName isEqualToString:@“class”]){
[self.classArr addObject:attributeDict[@“c_name”]];
self.GroupArr = [[NSMutableArray alloc]init];}
}
//结束标签 -
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@“遇到结束标签:%@”,elementName);
if([elementName isEqualToString:@“name”]){
self.model.name = self.str;
}else if ([elementName isEqualToString:@“age”]){
self.model.age = self.str;
}else if ([elementName isEqualToString:@“sex”]){
self.model.sex = self.str;
}else if ([elementName isEqualToString:@“student”]){
[self.GroupArr addObject:self.model];
}else if ([elementName isEqualToString:@“class”]){
//将该班级的包含所有学生的数组 添加到DataSource
[self.arr addObject:self.GroupArr];
}
}
//内容
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@“遇到内容:%@”,string);
self.str = string;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.arr.count;
} -
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arr[section]count];
} -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *oj = @“CELL”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:oj];
if(!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:oj];
}
cell.textLabel.text = [self.arr[indexPath.section][indexPath.row]name];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@,%@",[self.arr[indexPath.section][indexPath.row]age],[self.arr[indexPath.section][indexPath.row]sex]];
return cell;
} -
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 88;
} -
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.classArr[section];
}