• XML解析之SAX详解


    XML解析之SAX详解

    本文属于作者原创 http://www.cnblogs.com/ldnh/

    XML解析的五个步骤

    • 1、打开文档

    • (void)parserDidStartDocument:(NSXMLParser *)parser ;

    • 2、开始查找起始标签

    • (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;

    • 3、获取标签内容

    • (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

    • 4、查找结束标签

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

    • 5、查询文档结束

    • (void)parserDidEndDocument:(NSXMLParser *)parser {

    解析详细代码

    #import "ViewController.h"
    #import "JKPTrainInfo.h"
    @interface ViewController ()<UITableViewDataSource,NSXMLParserDelegate>
    
    @property (nonatomic,strong) NSMutableArray *dataList;
    
    @property (nonatomic,strong) UITableView *tableView;
    
    //因为下面多次要用,所以要建一个全局变量
    @property (nonatomic,strong) JKPTrainInfo *currentInfo;
    
    @property (nonatomic,strong) NSMutableString *muString;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //加载视图
    [self initUI];
    //加载数据
    [self loadData];
    
    }
    
    #pragma mark -getter and setter methods
    
    -(NSMutableArray *)dataList
    {
    if (!_dataList) {
      
      _dataList = [NSMutableArray array];
    }
    return _dataList;
    }
    
    -(UITableView *)tableView
    {
    if (!_tableView) {
      
      _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
      
      _tableView.dataSource = self;
    }
    return _tableView;
    }
    
    
    - (NSMutableString *)muString
    {
    if (!_muString) {
      _muString = [NSMutableString string];
    }
    
    return _muString;
    }
    
    #pragma mark - UITableView dataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * identifiter = @"cellID";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifiter];
    
    if (!cell) {
      
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifiter];
    }
    JKPTrainInfo *JkpInfo = self.dataList[indexPath.row];
    
    cell.textLabel.text = JkpInfo.trainStation;
    
    cell.detailTextLabel.text = JkpInfo.arriveTime;
    
    return cell;
    }
    
    
    
    #pragma mark - NSXMLParserDelegate 
    
    //1. 打开文件,开始解析
    -(void)parserDidStartDocument:(NSXMLParser *)parser
    {
    NSLog(@"打开文件,开始解析");
    
    //每次解析前进行清空操作-防止多次加载重复数据
    
    [self.dataList removeAllObjects];
    }
    
    //2. 查找起始标签
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
    
    
    //    NSLog(@"%@",elementName);
    //    
    //    NSLog(@"%@",attributeDict);
    
    if ([elementName isEqualToString:@"trainDetailInfo"]) {
      
      self.currentInfo = [[JKPTrainInfo alloc]init];
      
      [self.currentInfo setValuesForKeysWithDictionary:attributeDict];
    }
    
    //清空muString 数据
    self.muString.string = @"";
    }
    
    //3.获取标签内容
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
    //    NSLog(@"%@",string);
    
    //防止多次执行获取内容的方法
    [self.muString appendString:string];
    }
    
    // 4.查找结束标签
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
    //    NSLog(@"%@",elementName);
    
    if ([elementName isEqualToString:@"trainDetailInfo"]) {
      
      [self.dataList addObject:self.currentInfo];
    }
    
    else if (![elementName isEqualToString:@"dataSet"]&&![elementName isEqualToString:@"diffgr:diffgram"])
    {
      // self.muString是数据内容
      [self.currentInfo setValue:self.muString forKey:elementName];
    }
    
    }
    
    //5.解析完成
    -(void)parserDidEndDocument:(NSXMLParser *)parser
    {
    NSLog(@"解析完成");
    
    //解析完成后刷新数据
    [self.tableView reloadData];
    }
    
    #pragma mark - event handle methods
    
    - (void)loadData
    {
    NSString *urlString = @"http://192.168.1.68/train.xml";
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *requst = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
      
      //XML解析
      NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
      
      parser.delegate = self;
      
      [parser parse];
      
    }];
    
    }
    
    - (void)initUI
    {
    [self.view addSubview:self.tableView];
    }
    
    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    scala文件读取报错“java.nio.charset.MalformedInputException: Input length = 1”
    关于sparksql操作hive,读取本地csv文件并以parquet的形式装入hive中
    区别window.location.Reload()和window.location.href=window.location.href;
    datagrid GridView Repeater 绑定 序号 逐一递增的实现
    品牌机用Vista光盘分区
    网络路径结点回溯分析工具
    几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
    Ubuntu Linux 设置
    sql server中为某个整数前加上一定数量的0,如1,要返回00001
    本地测试域名
  • 原文地址:https://www.cnblogs.com/ldnh/p/5294605.html
Copyright © 2020-2023  润新知