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
原文地址:https://www.cnblogs.com/ldnh/p/5294605.html