SAX解析数据

1.Appdelegate里面设置根目录以及导航初始化主界面
2.解析本地xml文件
3.建model类声明属性
model类.m文件里
4.在viewcontroller里面代码如下,搭建界面建立表格

//


#import "ViewController.h"
#import "Student.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate,NSXMLParserDelegate>
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) NSMutableArray * dataSource;
@property(nonatomic,strong) Student * student;
@property(nonatomic,strong) NSString * string;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"XML SAX解析";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"解析" style:UIBarButtonItemStylePlain target:self action:@selector(SAX)];
    [self.view addSubview:self.tableView];

}
-(void)SAX{
    //SAX解析
    //创建url
    NSURL * url = [NSURL URLWithString:@"http://127.0.0.1/student1.xml"];
    //创建解析器 NSXMLParser
    NSXMLParser * parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
    //遵守协议 设置代理
    parser.delegate = self;
    //开始解析 
    [parser parse];


}
#pragma mark -- 协议方法
-(void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"开始解析文档");
    // 可以在开始的时候  初始化  数组
    self.dataSource = [[NSMutableArray alloc]init];

}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"已经结束解析文档");
    //刷新表格
    [self.tableView 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.student = [[Student alloc]init];
    }

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"遇到结束标签:%@",elementName);
    if ([elementName isEqualToString:@"name"]) {
        self.student.name = self.string;
    }else if ([elementName isEqualToString:@"age"]){
        self.student.age = self.string;
    }else if ([elementName isEqualToString:@"sex"]){
        self.student.sex = self.string;
    }else if ([elementName isEqualToString:@"student"]){
        //学生的 结束标签  将学生添加到  数组里
        [self.dataSource addObject:self.student];
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"遇到内容:%@",string);

    self.string = string;

}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate  = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    //赋值
    cell.textLabel.text = [self.dataSource[indexPath.row] name];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"age = %@,sex=%@",[self.dataSource[indexPath.row] age],[self.dataSource[indexPath.row] sex]];

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}



@end

猜你喜欢

转载自blog.csdn.net/chuck_phonics/article/details/81782580