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. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  2. HDU3033I love sneakers!(分组背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3033 本题的意思就是说现在有n种牌子的鞋子,每种品牌有一些不同的鞋,每双鞋子都有一个特定的权值,现在要求每种品牌 ...

  3. newlsip 检查磁盘分区使用情况

    主要还是用df -k这个命令,然后将输出结果全部逐行解析,最后调用REST API,发送给服务器保存. 参考代码: #!/usr/bin/newlisp (set 'cur-path "/o ...

  4. logstash开源日志收集查询分析系统

    http://storysky.blog.51cto.com/628458/1158707/ http://www.logstash.net/ http://blog.sina.com.cn/s/bl ...

  5. Android 图标上面添加提醒使用开源UI类库 Viewbadger

    Viewbadger 1.BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及 ...

  6. url中的scheme

    iPhone上URL Schemes的作用为应用程序提供了一个其他应用程序或者safari可以启动他的方法. --http://blog.sina.com.cn/s/blog_5673c12f0100 ...

  7. .NET开发中的事务处理大比拼

    本文转载:http://www.cnblogs.com/jhxk/articles/2696307.html http://liubaolongg.blog.163.com/blog/static/2 ...

  8. MySQL通配符过滤

    在WHERE后使用LIKE操作符能够进行通配符过滤: products表例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1eWluZ18xM ...

  9. 【转】C++ 异常

    一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常处理,以及异常处理的基本思想 C++之父Bjarne Stroustrup在<The C++ Programming ...

  10. defer属性---->执行外部脚本

    HTML4---->只有IE支持 不需要是外部脚本. HTML5---->主流都支持 defer 属性仅适用于外部脚本(只有在使用 src 属性时) 值 描述 defer 规定当页面已完成 ...