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

XML解析之SAX详解的更多相关文章

  1. XML解析之DOM详解及与SAX解析方法的比较

    XML解析(DOM) XML文件解析方法介绍 我们所用到的NSXMLParser是采用SAX方法解析 SAX(Simple API for XML) 只能读,不能修改,只能顺序访问,适合解析大型XML ...

  2. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  3. 谷歌地图地理解析和反解析geocode.geocoder详解

    地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. 地理反解析和上面的过程相反是将地理坐标(如纬度:26.57,经度:106.71)转换为地址(中国 ...

  4. XML参考 :XmlReader 详解、实例

    XML参考 :XmlReader 详解.实例-- 详解 转:http://www.cnblogs.com/Dlonghow/archive/2008/07/28/1252191.html XML参考 ...

  5. Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  6. 浏览器解析html全过程详解

    前端文摘:深入解析浏览器的幕后工作原理 关于浏览器解析html全过程详解 输入URL到浏览器接收返回的数据的整个过程 TCP报文格式详解 IP报文格式详解 Linux IO模式及 select.pol ...

  7. 谷歌地图地理解析和反解析geocode.geocoder详解(转)

    谷歌地图地理解析和反解析geocode.geocoder详解 谷歌Geocoder服务 实例代码 地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. ...

  8. XML解析(二) SAX解析

    XML解析之SAX解析: SAX解析器:SAXParser类同DOM一样也在javax.xml.parsers包下,此类的实例可以从 SAXParserFactory.newSAXParser() 方 ...

  9. PULL解析XML的运行机制详解

    PULL解析简单易上手,基本上看一遍,基本上就会解析啦,但总是感觉对PULL解析的运行机制不是很了解,就总结了以下事件驱动到底是怎么执行的.. PULL: Android内置了PULL解析器.PULL ...

随机推荐

  1. python数据库(mysql)操作

    一.软件环境 python环境默认安装了sqlite3,如果需要使用sqlite3我们直接可以在python代码模块的顶部使用import sqlite3来导入该模块.本篇文章我是记录了python操 ...

  2. ROS 常用命令字典

    版权声明:本文为博主原创文章,转载请标明出处: http://www.cnblogs.com/liu-fa/p/5761448.html 该博文适合已经具备一定的ROS编程基础的人,快速查看ROS相关 ...

  3. ES6笔记(2)-- let的块级作用域

    系列文章 -- ES6笔记系列 一.函数级作用域 我们都知道,在ES6以前,JS只有函数级作用域,没有块级作用域这个概念 没有块级作用域,有利有弊,利于方便自由,弊于作用域内的变量容易被共享,例如这个 ...

  4. SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较

    排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数: .row_number .rank .dense_rank .ntile 下面分别介绍一下这四个 ...

  5. JAVA - HashMap和HashTable

    1. HashMap 1)  hashmap的数据结构 Hashmap本质就是一个数组,只是当key值重复时,使用链表的方式来存储重复的key值(拉链法),注意:链表中存放的仍然是key值.如下图示: ...

  6. 代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能

    在代码生成工具的各种功能规划中,我们一向以客户的需求作为驱动,因此也会根据需要增加一些特殊的功能或者处理.在实际的开发中,虽然我们一般以具体的表进行具体业务开发,但是有些客户提出有时候视图开发也是很常 ...

  7. IN31志愿者“孝行天下,感恩父母”晚会

    IN31是一群志愿者,为社会倾力奉献与引发爱的公益组织.成功举办第一场孝行天下的大型公益活动

  8. C#的变迁史 - C# 4.0篇

    C# 4.0 (.NET 4.0, VS2010) 第四代C#借鉴了动态语言的特性,搞出了动态语言运行时,真的是全面向“高大上”靠齐啊. 1. DLR动态语言运行时 C#作为静态语言,它需要编译以后运 ...

  9. Firemonkey 在 iOS 平台能显示更多的 emoji 字符

    使用 Firmonkey 在显示 emoji 字符时,有些 emoji 并无法显示彩色,见下图: 经查 FMX 源码,是因为判断 emoji 的字符区段不足造成的,经过修改后,便可显示,见下图: 修改 ...

  10. No.018:4Sum

    问题: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...