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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值