iOS 中的XML解析代码(SAX)

1、XML解析(SAX)

NSXMLParser SAX 大文件

1)打开文档

- (void)parserDidStartDocument:(NSXMLParser *)parser

2)开始查找起始标签

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict

开始元素 : elementName
元素属性 : attributeDict

3)获取标签内容

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

获取内容 : string

4)查找结束标签

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

结束元素 : elementName namespaceURI 和 qName 为空值

5)关闭文档(查询结束)


- (void)parserDidEndDocument:(NSXMLParser *)parser

实现原理:

  • 1.在查找起始标签的时候,判断elementName是否与对应的字符串相等,并将接收到的内容删除

  • 2.因为获取内容的时候会出现获取两次 则使用追加方法,防止多次执行获取内容的方法 string + @“ "

  • 3.在查找结束标签的时候,判断elementName是否与对应的字符串相等,并将获取的模型加载到可变数组中,作为数据源,进行其他操作,再判断是否与对应的字符串不相等,利用kvc将对应的结束标签作为KEY值,对应获取的内容作为value值

//
//  ViewController.m
//  
//
//  Created by peng on 16/2/19.
//  Copyright (c) 2016年 pss. All rights reserved.
//

#import "ViewController.h"
#import "PSSTrainInfo.h"

static NSString *identifier = @"cellID";

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataList;

@property (nonatomic, strong) PSSTrainInfo *model;

@property (nonatomic, strong) NSMutableString *muString;

@end

@implementation ViewController

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

- (NSMutableString *)muString {
    
    if (!_muString) {
        _muString = [NSMutableString string];
    }
    return _muString;
}

- (NSMutableArray *)dataList {
    
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

- (PSSTrainInfo *)model {
    
    if (!_model) {
        _model = [[PSSTrainInfo alloc] init];
    }
    return _model;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
    
    
    return cell;
}

- (void)parserDidStartDocument:(NSXMLParser *)parser  {
    
    NSLog(@"开始解析");
    
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    
    NSLog(@"起始元素:%@", elementName);
    
    if ([elementName isEqualToString:@"trainDetailInfo"]) {
        
        [self.model setValuesForKeysWithDictionary:attributeDict];
        
    }
    self.muString = nil;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    
    NSLog(@"内容:%@", string);
    
    [self.muString appendString:string];
    
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    NSLog(@"结束元素:%@", elementName);
    
    if ([elementName isEqualToString:@"trainDetailInfo"]) {
        
        [self.dataList addObject:self.model];
        
    }else if (![elementName isEqualToString:@"dataSet"] && ![elementName isEqualToString:@"diffgr:diffgram"]) {
        
        [self.model setValue:self.muString forKey:elementName];
        
    }
    
    
    
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    
    NSLog(@"结束解析");
    
    [self.tableView reloadData];
    
    NSLog(@"%@",self.dataList);
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://192.168.1.2/train.xml"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       
        
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
        
        parser.delegate = self;
        
        [parser parse];
        
        
    }];
    
    [self.view addSubview:self.tableView];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(iOS 中的XML解析代码(SAX))