UI课程15 XML、JSON数据结构解析
来源:互联网 发布:垂帘听政知乎 编辑:程序博客网 时间:2024/05/18 22:51
1.解析
1)解析:从事先规定好的格式中提取数据
2)前提:提前约定好格式,数据提供方按照格式提供数据,获取方按照格式获取数据
3)XML(Extensible Markup Laguage 可扩展标记语言),数据解析时用到SAX工具,DOM工具
JSON(Javascript object Notation)数据结构解析时用到第三方框架
2.XML
1)功能:用来存储和传输数据。
2)语法:节点使用一对标签表示
只有一个根节点
值存储在一对标签中间
eg:
<students> <student> <name>john</name> <age>18</age> <gender>male</gender> <hobby>play</hobby> <phone>15256778679</phone> </student></students>
3)SAX解析:(Simple API for XML)(系统的)基于事件驱动的解析方式,逐行解析数据(采用协议回调机制),NSXMLParser是iOS自带的XML解析类。
4)DOM解析:(Document Object Model)(第三方),文档对象模型。解析时需将XML文件整体读入,并且将XML结构化成树状,通过树状读取数据
GDataXMLNode是Google提供的开源XML解析类,解析速度比NSXMLParser快
3.JSON
轻量级的数据交换格式,完全独立于语言的文本格式
1)语法:有两种结构:对象、数组
对象:写在 { … } 中,是“键:值”对儿的集合,多个对儿之间逗号隔开,类似字典
数组:写在 [ … ] 中,以值之间以逗号分开
2)JSON中的数据类型:字符串,数值,BOOL,对象,数组
eg:[
{
“name”:”Anna”,
“age”:20,
“gender”:”female”,
“hobby”:”play”,
“phone”:”18028074567”
},
{
“name”:”Tim”,
“age”:24,
“gender”:”male”,
“hobby”:”sleep”,
“phone”:”13026572367”
}
]
3)使用Foundation进行解析(系统的)
工具:NSJSONSerialization对象(效率最高)
4)使用JSONKit解析
4.四中方法的代码详例
#pragma mark - JSON JSONKit 解析(第三方)- (IBAction)jsonKitAction:(id)sender { //1.获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"JSON_StudentInfo" ofType:@"txt"]; //2.创建data对象 NSData *data = [NSData dataWithContentsOfFile:path]; //3.解析 NSArray *array = [data objectFromJSONData]; //4.组装 _array = [[NSMutableArray alloc] initWithCapacity:6]; for (NSDictionary *dic in array) { Student *stu = [Student new]; [stu setValuesForKeysWithDictionary:dic]; [_array addObject:stu]; } //5.校验 for (Student *stu in _array) { NSLog(@"%@",stu); }}#pragma mark - JSON Foundation 解析(系统)- (IBAction)jsonFoudationAction:(id)sender { //1.获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"JSON_StudentInfo" ofType:@"txt"]; //2.创建data对象 NSData *data = [NSData dataWithContentsOfFile:path]; //3.解析(拿到后台的数据,类型取决于后台数据) NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];// NSLog(@"%@",array); //4.组装(将文件中的数组与我们的数组关联,每一个) _array = [[NSMutableArray alloc] initWithCapacity:6]; for (NSDictionary *dic in array) { Student *stu = [Student new]; [stu setValuesForKeysWithDictionary:dic]; [_array addObject:stu]; } //5.校验 for (Student *stu in _array) { NSLog(@"%@",stu); }}#pragma mark - XML DOM 解析(第三方)- (IBAction)xmlDOMAction:(id)sender { //1.获取文件路径 NSBundle *bundle = [NSBundle mainBundle]; // 获取当前文件应用包 NSString *path = [bundle pathForResource:@"StudentInfo_XML" ofType:@"txt"]; //2.根据文件路径创建data对象 NSData *data = [NSData dataWithContentsOfFile:path]; //3.根据data对象创建解析对象 GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //4.获取根节点 GDataXMLElement *rootElement = [document rootElement]; _array = [[NSMutableArray alloc] initWithCapacity:4]; for (GDataXMLElement *subElement in rootElement.children) { Student *student = [Student new]; for (GDataXMLElement *element in subElement.children) { [student setValue:element.stringValue forKey:element.name]; } [_array addObject:student]; } //校验 for (Student *stu in _array) { NSLog(@"%@",stu); }}#pragma mark - 代理回调机制#pragma mark - XML SAX 解析(系统)- (IBAction)xmlSAXAction:(id)sender { //反复执行,开始解析(使用代理) //1. 获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfo_XML" ofType:@"txt"]; //2.根据文件路径创建data对象 NSData *data = [[NSData alloc] initWithContentsOfFile:path]; //3.根据data对象创建解析对象 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //4.设置代理对象 [parser setDelegate:self]; //5.开始解析 [parser parse];}#pragma mark - SAX 代理步骤//1.开始文档解析- (void)parserDidStartDocument:(NSXMLParser *)parser{ //准备数组 (数组中存的是指针)(当存储空间不够时,会以上一个的存储空间大小为基础,增加2倍) _array = [[NSMutableArray alloc] initWithCapacity:4];}//2.开始标签解析- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ //记录当前节点标签 _currentElement = elementName; //根据标签建立model if ([elementName isEqualToString:@"student"]) { Student *student = [Student new]; [_array addObject:student]; }}//3.获取标签内容- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ //获取数组的最后一个元素 Student *student = [_array lastObject]; //KVC赋值 [student setValue:string forKey:_currentElement];}//4.结束标签解析- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ //当前存储节点置为空,不影响下次判断 _currentElement = nil;}//5.结束文档解析(可以不写,但文档解析完后,会走这个方法)- (void)parserDidEndDocument:(NSXMLParser *)parser{ //校验数据 for (Student *student in _array) { NSLog( @"%@",student); }}//6.错误处理(可以不写)- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{ NSLog(@"错误为%@",parseError);}
- UI课程15 XML、JSON数据结构解析
- UI XML,JSON 数据结构解析
- UI第十五天:XML、JSON数据结构解析
- UI基础-XML、JSON 数据结构解析
- UI day 14 XML JSON数据结构解析
- XML.JSON数据结构解析
- XML、JSON数据结构解析
- XML,JSON数据结构解析
- UIday1502:XML 、JSON 数据结构解析
- [TwistedFate]XML JSON数据结构解析
- UI一揽子计划 15 (XML的解析方式 (SAX解析/ DOM解析) 、JSON 解析)
- IOS--UI--LessonParse 数据解析 XML JSON
- UI进阶:数据解析(XML解析和JSON解析)
- 第十五讲:XML、JSON数据结构解析
- iOS之JSON,XML数据结构解析
- iOS编程------XML、JSON数据结构解析
- UI进阶——XML解析与Json解析
- Android Json解析课程
- oracle存储过程中is和as的区别?
- Java8揭秘(一)
- [LeetCode] 7.Reverse Integer
- Android使用Fragment实现标签页
- zoj3609 Modular Inverse(扩展欧几里德算法)
- UI课程15 XML、JSON数据结构解析
- Java8揭秘(二)Java 8中的 Lambda表达式
- jsp中的page指令(属性)
- String.Split()函数
- Sublime Text 2 - There are no packages available for installation 解决方法(亲测可行)
- 模板与图像相似性
- NFS挂载文件系统需要的内核和uboot的配置
- ffmpeg解复用解码重采样
- 1007. 素数对猜想 (20)