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. 建立ODBC数据源(基于windows)

    1. win+r 2. control 3. 打开数据源 4. 点击添加 5. 选择Oracle in OraClient11g_home1 ,点击完成 6. 填写,查看具体参数信息点击Help 7. ...

  2. RS232串口用事件接受数据(一问一答)

    private void button1_Click(object sender, EventArgs e) { serialPort1.Open(); serialPort1.DataReceive ...

  3. UITextfiled子视图被剪切

    一般情况下,如果在view1上面添加了view2,但是view2超出了view1,也会在屏幕上面显示超出的部分 例如: UIButton *button =[[UIButton alloc]initW ...

  4. 消息系统Kafka介绍 - 董的博客

    1.  概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...

  5. 微信get post请求到微信服务器 模版 素材操作

    1:素材管理 官方文档 package org.konghao.weixin.media; import java.io.File; import java.io.IOException; impor ...

  6. 对于C语言中数组名是指针的理解

    我们都知道,c语言中数组名是一个指针,比如下面这段代码 #include<iostream>using namespace std;int main(){ int a[4]={1,2,3, ...

  7. CMSIS Example - Signal and Yield

    /*---------------------------------------------------------------------------- * RL-ARM - RTX *----- ...

  8. IAR Build from the command line 环境变量设置

    http://supp.iar.com/Support/?Note=47884 Technical Note 47884 Build from the command line The alterna ...

  9. cocos2d jsb 打包 Android APK

    1.首先要会普通的cpp 打包成Android APK 下面所说的是在cocos2d-x 2.2.2 或者 2.3 版本号中.本文在Eclipse总用ndk编译cocos2d-x. 老生常谈cocos ...

  10. 【M33】将非尾端类设计为抽象类

    1.考虑下面的需求,软件处理动物,Cat与Dog需要特殊处理,因此,设计Cat和Dog继承Animal.Animal有copy赋值(不是虚方法),Cat和Dog也有copy赋值.考虑下面的情况: Ca ...