欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SAX解析

程序员文章站 2022-05-28 08:04:56
...

1.创建Student 继承NSObject
2.在Student.h中写入属性

@property(nonatomic,strong)NSString *name,*age,*sex;

3.A.m中写入导航


#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
   self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
    
    
    
    return YES;
}

4.v.m中写入解析内容

#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{
    
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/student.xml"];
    //创建路径
    //创建NSXMLParser解析器
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    //遵守协议  设置代理
    parser.delegate = self;
    
    //开始解析
    [parser parse];
    
}
#pragma mark - NSXMLParserDelegate 方法

//开始解析元素
- (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;
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
    
    
}
//设置cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        
    }
    //设置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;
}



@end

相关标签: SAX解析