XML解析之DOM详解及与SAX解析方法的比较
XML解析(DOM)
XML文件解析方法介绍
我们所用到的NSXMLParser是采用SAX方法解析
- 只能读,不能修改,只能顺序访问,适合解析大型XML,解析速度快
- 常应用于处理大量数据的XML,实现异构系统的数据访问,实现跨平台
- 从文档的开始通过每一节点移动,定位一个特定的节点
- 不仅能读,还能修改,而且能够实现随机访问,缺点是解析速度慢,适合解析小型文档
- 一般应用与小型的配置XML,方便操作
- 为载入到内存的文档节点建立类型描述,呈现可横向移动、潜在巨大的树型结构
- 在内存中生成节点树操作代价昂贵
NSXMLParser解析
1. 开始解析某个元素,会遍历整个XML,识别元素节点名称
- (void)parser:didStartElement:namespaceURI:qualifiedName:attributes:
2. 文本节点,得到文本节点里存储的信息数据,对于大数据可能会接收多次!为了节约内存开销
- (void)parser:foundCharacters:
3. 结束某个节点,存储从parser:foundCharacters:方法中获取到的信息
- (void)parser:didEndElement:namespaceURI:qualifiedName:
注意:在解析过程中,上述三个方法会不停的重复执行,直到遍历完成为止
4. 开始解析XML文档
- (void)parserDidStartDocument:
5. 解析XML文档结束
- (void)parserDidEndDocument:
6. 解析出错
XML(DOM)具体步骤
1.在github上下载GDataXMLNode
2.配置第三方
3.使用GDataXMLNode解析XML数据
- 下载大家应该都会把,不用我教,直接来第二步
点击工程名字,然后如下图所示添加代码,然后我们的第三方库就可以使用了


第三步:进行XML解析数据-这儿直接上代码,注释也挺详细的,相信你们都可以看懂
viewCOntroller
//
// ViewController.m
// XML(DOM解析)
//
// Created by ma c on 16/3/19.
// Copyright (c) 2016年 姬凯鹏. All rights reserved.
// #import "ViewController.h"
#import "JKPTrainInfo.h"
#import "GDataXMLNode.h"
@interface ViewController ()<UITableViewDataSource> @property (nonatomic, strong) NSMutableArray * dataList; @property (nonatomic,strong) UITableView *tableView; @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;
} #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 - event handle methods
// 载入数据
- (void)loadData
{
NSURL *url = [NSURL URLWithString:@"http://192.168.1.68/train.xml"]; NSURLRequest *requst = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //根据数据生成xml解析文档
GDataXMLDocument *gData = [[GDataXMLDocument alloc]initWithData:data error:nil];
//获取xml根节点下所有子节点
for (GDataXMLElement *element in gData.rootElement.children) { //获取子节点下的孙子节点
for (GDataXMLElement * trainInfo in element.children) { //创建模型
JKPTrainInfo *info = [[JKPTrainInfo alloc]init]; // 经检测 stringValue是数据 name是节点的名字 GDataXMLElement是节点下的数据部分 //即这个<trainStation>上海南(车次:T81\T80)</trainStation>
for (GDataXMLElement *note in trainInfo.children) { [info setValue:note.stringValue forKey:note.name]; // NSLog(@"%@",note.stringValue);
//
// NSLog(@"-----------");
//
// NSLog(@"%@",note.name);
} //GDataXMLNode *att 是XML文件中这个 <trainDetailInfo info="trainDetailInfo1" rowOrder="0" hasChanges="inserted">
for (GDataXMLNode *att in trainInfo.attributes ) { [info setValue:att.stringValue forKey:att.name]; NSLog(@"%@",att.stringValue); NSLog(@"-----------"); NSLog(@"%@",att.name); }
//将模型加入到数据中
[self.dataList addObject:info];
} }
//刷新数据 一定要在异步请求里面刷新数据 不然不好使
[self.tableView reloadData]; }];
} //载入视图
- (void)initUI
{
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
mode类
//
// JKPTrainInfo.h
// XML解析(SAX)练习
//
// Created by ma c on 16/3/19.
// Copyright (c) 2016年 姬凯鹏. All rights reserved.
// #import <Foundation/Foundation.h> @interface JKPTrainInfo : NSObject
//解析数据
@property (nonatomic, copy) NSString * info;
@property (nonatomic, copy) NSString * rowOrder;
@property (nonatomic, copy) NSString * hasChanges; @property (nonatomic, copy) NSString * trainStation;
@property (nonatomic, copy) NSString * arriveTime;
@property (nonatomic, copy) NSString * startTime;
@property (nonatomic, copy) NSString * KM; @end
XML解析之DOM详解及与SAX解析方法的比较的更多相关文章
- Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)
Android XML shape 标签使用详解 一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...
- Day04 dom详解及js事件
day04 dom详解 DOM的基础 Document对象 Element对象 Node对象 innerHTML 事件处理 表单验证 上次课内容回顾: JS中ECMAScript用法: JS定义变 ...
- SpringBoot之DispatcherServlet详解及源码解析
在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...
- JavaScript进阶内容——DOM详解
JavaScript进阶内容--DOM详解 当我们已经熟练掌握JavaScript的语法之后,我们就该进入更深层次的学习了 首先我们思考一下:JavaScript是用来做什么的? JavaScript ...
- 谷歌地图地理解析和反解析geocode.geocoder详解
地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. 地理反解析和上面的过程相反是将地理坐标(如纬度:26.57,经度:106.71)转换为地址(中国 ...
- XML参考 :XmlReader 详解、实例
XML参考 :XmlReader 详解.实例-- 详解 转:http://www.cnblogs.com/Dlonghow/archive/2008/07/28/1252191.html XML参考 ...
- 虚拟DOM详解
虚拟DOM简介 Virtual Dom可以看做一棵模拟了DOM树的JavaScript对象树,其主要是通过vnode,实现一个无状态的组件,当组件状态发生更新时,然后触发Virtual Dom数据的变 ...
- Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)
Android XML shape 标签使用详解 一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...
- 浏览器解析html全过程详解
前端文摘:深入解析浏览器的幕后工作原理 关于浏览器解析html全过程详解 输入URL到浏览器接收返回的数据的整个过程 TCP报文格式详解 IP报文格式详解 Linux IO模式及 select.pol ...
随机推荐
- BZOJ1088扫雷Mine 解析报告
1088: [SCOI2005]扫雷Mine Description 相信大家都玩过扫雷的游戏.那是在一个n*m的矩阵里面有一些雷,要你根据一些信息找出雷来.万圣节到了,“余”人国流行起了一种简单的扫 ...
- 解决matplotlib的中文问题
matplotlib的强大无需我去言说,但它对使用中文的我来说却有一点瑕疵,那就是--在默认状态下,matplotlb无法在图表中使用中文. 在网上查找了一些资料,发现matplotlib本身是支持U ...
- .Net语言 APP开发平台——Smobiler学习日志:用MenuView控件仿钉钉APP的首页菜单
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...
- "Hello World!" for the NetBeans IDE
"Hello World!" for the NetBeans IDE It's time to write your first application! These detai ...
- jquery属性选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jquery属性选择器(匹配具有指定属性的元素)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
最近下载一个新版本的adt-bundle,Android API是20. 把Plain Text控件往布局上面拖时,发现拖不上去,出现了下面的错误: Exception raised during r ...
- archive for required library...
最近把移动硬盘上的一个Android项目复制到笔记本上面,import后项目文件夹始终有一个红色叹号,console里面提示“archive for required library...”,原来是l ...
- 再探OAuth2
原文: http://www.cnblogs.com/Irving/p/4134629.html web:http://oauth.net/2/ rfc: http://tools.ietf.org/ ...
- bitbucket+sourcetree+p4merge for windows 版本控制
这里选择bitbucket作为仓库的原因是,它能够在设置私有仓库的前提下组建5人团队 一:https://bitbucket.org/ 注册bitbucket 二:http://www.sourcet ...