XML解析之SAX詳解 本文屬於作者原創 http://www.cnblogs.com/ldnh/ XML解析的五個步驟 1、打開文檔 (void)parserDidStartDocument:(NSXMLParser )parser ; 2、開始查找起始標簽 (void)parser:(NSXML
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