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)的更多相关文章

  1. iOS 中的 xml 解析

    在ios 中解析xml 的方法有很多种 1.苹果原生 NSXMLParser:SAX方式解析,使用简单 2.第三方框架 libxml2:纯c语言,默认包含在ios  sdk中,同时支持DOM 和 SA ...

  2. IOS中的XML解析之DOM和SAX

    一.介绍 dom是w3c指定的一套规范标准,核心是按树形结构处理数据,dom解析器读入xml文件并在内存中建立一个结构一模一样的“树”,这树各节点和xml各标记对应,通过操纵此“树”来处理xml中的文 ...

  3. iOS - - JSON 和 XML解析

    JSON 和 XML 一.JSON 1.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) 2.JSON的格 ...

  4. XML解析(二) SAX解析

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

  5. XML解析之SAX详解

    XML解析之SAX详解 本文属于作者原创 http://www.cnblogs.com/ldnh/ XML解析的五个步骤 1.打开文档 (void)parserDidStartDocument:(NS ...

  6. ios之json,xml解析

    JSON解析步骤: 1.获取json文件路径 NSString*path = [[NSBundle mainBundle] pathForResource:@"Teacher"of ...

  7. 解决在php5中simple XML解析错误的问题

    2004年7月,php5正式版本的发布,标志着一个全新的PHP时代的到来.PHP5的最大特点是引入了面向对象的全部机制,并且保留了向下的兼容性.程序员不必再编写缺乏功能性的类,并且能够以多种方法实现类 ...

  8. iOS中UIWebView执行JS代码(UIWebView)

    iOS中UIWebView执行JS代码(UIWebView) 有时候iOS开发过程中使用 UIWebView 经常需要加载网页,但是网页中有很多明显的标记让人一眼就能看出来是加载的网页,而我们又不想被 ...

  9. iOS边练边学--iOS中的XML数据解析

    XML的解析方式 SAX 大小文件都可以 NSXMLParser DOM 最好是小文件 GDataXML NSXMLParser的用法 创建解析器来解析 // 创建XML解析器 NSXMLParser ...

随机推荐

  1. POJ3974 Palindrome (manacher算法)

    题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...

  2. InitializingBean和init-method

    [spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系] InitializingBean Spring的Initi ...

  3. 11.聚合(Aggregation)

    聚合关系是关联关系的一种特例,它体现的是整体与部分的关系,即has-a的关系,此时整体与部分之间是可分离的,它们可以具有各自的生命周期.比如计算机与CPU.公司与员工的关系等.表现在代码层面,和关联关 ...

  4. 【css hack】正是我所找的,帮了大忙啊

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-03-05) 各个浏览器单独设置属性 IE6:能识别下划线 “_” 和 星号 “*“,不能识别 “!important”    ...

  5. 浅谈Oracle函数返回Table集合

    在调用Oracle函数时为了让PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合 ...

  6. 推荐第三方Oracle客户端查询工具

    1.SqlDbx 官方地址:http://www.sqldbx.com/personal_edition.htm 2.devart http://www.devart.com/dbforge/orac ...

  7. JS函数的定义与调用方法

    JS函数调用的四种方法:方法调用模式,函数调用模式,构造器调用模式,apply,call调用模式 1.方法调用模式:先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来 ...

  8. [Jobdu] 题目1527:首尾相连数组的最大子数组和

    题目描述: 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是相连的.数组中一个或多个连续元素可以组成一个子数组,其中存在这样的子数组arr[i],…arr ...

  9. 64位ubuntu下装32位软件

    本帖最后由 wuy069 于 2013-10-25 12:28 编辑 很多软件只有32位的,有的依赖32位库还挺严重的:从ubuntu 13.10已经废弃了ia32-libs,但可以使用多架构,安装软 ...

  10. 32位Windows7上8G内存使用感受+xp 32位下使用8G内存 (转)

    32位Windows7上8G内存使用感受+xp 32位下使用8G内存 博客分类: Windows XPWindowsIE企业应用软件测试  我推荐做开发的朋友:赶快加入8G的行列吧....呵呵..超爽 ...