(一) 解析: 从事先规定好的格式中提取数据; 前提:提前约定好格式,数据提供方按照格式提供数据,数据获取方则按照格式获取数据. 常见的解析:XML JSON. 前四个页面.当点击页面时触发效果.所以代码写在touchsBegan中 //第五个页面(viewController) : (二)XML 数据结构 (主要用于天气预报) XML: Extensible Markup language (可扩展标记语言);可用来存储和传输数据; 功能:数据交换 ,, 内容管理 ,, 用作配置文件 语法: (1) 声明 (2) 节点使用一对标签表示: 起始和结束标签. (3) 根节点是起始节点,只有一个,节点可以嵌套. (4) 节点可以有值,存储在一对标签中. 示例: <student> <name>王大</name> <age>22</age> </student> (三) : 进行XML解析时使用到的SAX工具; SAX: Simple API for XML .基于事件驱动的解析方式, 逐行解析数据. (采用协议回调机制) (1) NSXMLParser 是IOS自带的XML解析类.采用SAX方式解析数据.. (2) 解析过程由NSXMLParserDelegate协议方式回调 (3) 解析过程 : 开始标签->取值->结束标签. (四) : 进行XML解析时使用到的DOM工具 (五)JOSN数据结构 百度 : 在线代码格式化 JSON (javaScript Object Notation) 是一种轻量级的数据交换格式. JOSN数据结构的功能: (1) : 数据交换 (2) : 内容管理 (3) : 配置文件 (六) 优缺比较 XML JSON 上代码: AppDelegate.m self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; ViewController *firstVC = [[ViewController alloc] init]; firstVC.title = @"1"; SecondViewController *secondVC = [[SecondViewController alloc] init]; secondVC.title = @"2"; ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; thirdVC.title = @"3"; FourViewController *fourVC = [[FourViewController alloc] init]; fourVC.title = @"4"; FifthViewController *fifthVC = [[FifthViewController alloc] init]; fifthVC.title = @"5"; UITabBarController *tabBarVC = [[UITabBarController alloc] init]; tabBarVC.viewControllers = @[firstVC, secondVC, thirdVC, fourVC, fifthVC]; //设置TabBar上字体的大小和颜色. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor cyanColor],UITextAttributeTextColor,[UIFont systemFontOfSize:30] ,UITextAttributeFont, nil]; [[UITabBarItem appearance]setTitleTextAttributes:dic forState:(UIControlStateNormal)]; self.window.rootViewController = tabBarVC; (1).XML之SAM工具 Student.h @interface Student : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *gender; @property (nonatomic, strong) NSString *hobby; @property (nonatomic, assign) NSInteger age; Student.m @implementation Student //使用KVC时要用 - (void)setValue:(id)value forUndefinedKey:(NSString *)key { } ViewController.m #import "ViewController.h" #import "Student.h" //NSXMLParserDelegate 协议 , 是IOS中使用系统的SAX解析使用的协议. @interface ViewController () <NSXMLParserDelegate> //数据源数组 @property (nonatomic, strong) NSMutableArray *dataArray; //当前解析的标签 @property (nonatomic, copy) NSString *currentElement; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //[NSBundle mainBundle]代表的是左边那个文件夹栏. //pathForResource:@"文件名" ofType:@"文件格式"]. // 获取XML_stu.txt文件的路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"XML_stu" ofType:@"txt"]; // 通过文件路径 , 创建一个NSData对象 NSData *data = [NSData dataWithContentsOfFile:path]; // SAX解析使用的类. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; // 指定代理人 parser.delegate = self; //开始解析 [parser parse]; } //开始解析文档 -(void)parserDidStartDocument:(NSXMLParser *)parser { #warning 初始化数据源数组 self.dataArray = [NSMutableArray array]; } //检测到开始标签 //一参: 类 //二参: elementName 标签的名字 //以下三个 基本不会使用到 , 所以基本为 nil //三参: 节点里面的命名空间 (xmlns) //四参: qName //五参: attrubuteDict 标签的属性 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict { #warning 给currentElement赋值. #warning 如果标签的名字是student, 则表示 要创建一个model, 并添加到数据源数组里面 self.currentElement = elementName; if ([self.currentElement isEqualToString:@"student"]) { Student *student = [Student new]; [self.dataArray addObject:student]; } //NSLog(@"element ==== %@", elementName); } //检测到标签中的值 //一参: 类parser //二参: 值string -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { #warning 将检测到的值,存入model的具体属性 //#warning 给哪个属性赋值? //#warning 给哪个model赋值? 数组最后一个model赋值 Student *student = self.dataArray.lastObject; [student setValue:string forKey:self.currentElement]; // NSLog(@"string ==== %@", string); } // 检测到结束标签 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { //NSLog(@"endElement ==== %@", elementName); self.currentElement = nil; } //结束解析文档 -(void)parserDidEndDocument:(NSXMLParser *)parser { for (Student *model in self.dataArray) { NSLog(@"姓名%@ 性别%@ 爱好%@ 享年%ld岁", model.name, model.gender, model.hobby, model.age); } } XML之DOM 下面那两个后边有-fno-onjc-arc的,与拉进来的文件有关. -fno-onjc-arc作用: 告诉系统 在MRC情况下,使用arc,,系统就不要管了. Student.h @interface Student : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *gender; @property (nonatomic, strong) NSString *hobby; @property (nonatomic, assign) NSInteger age; Student.m @implementation Student //使用KVC时要用 - (void)setValue:(id)value forUndefinedKey:(NSString *)key { } SecondViewController.m #import "SecondViewController.h" #import "GDataXMLNode.h" #import "Student.h" @interface SecondViewController () @property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSString *path = [[NSBundle mainBundle]pathForResource:@"XML_stu" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSError *error = nil; self.dataArray = [NSMutableArray array]; // GDataXMLDocument XML中DOM解析的类 // 一参: 解析的data对象 // 二参: 一般是0 // 三参: error信息 GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:data options:0 error:&error]; // 获取XML数据的根节点 //获取到根节点里面所有的东西, 也就是说, 获取到students里面包含的所有东西 GDataXMLElement *rootElement = [document rootElement]; //NSLog(@"rootElement ==== %@", rootElement); for (GDataXMLElement *subElement in rootElement.children) { //这里获取到的subElement保存的是student标签里的内容, 所以在这里创建对象 Student *student = [Student new]; for (GDataXMLElement *element in subElement.children) { //NSLog(@"%@", element); //element 保存的就是student 标签的所有子标签. //根据标签的名字给属性赋值. // element.name 表示标签的名字 // element.stringValue 表示标签的值. if ([element.name isEqualToString:@"name"]) { student.name = element.stringValue; } if ([element.name isEqualToString:@"gender"]) { student.gender = element.stringValue; } if ([element.name isEqualToString:@"hobby"]) { student.hobby = element.stringValue; } if ([element.name isEqualToString:@"age"]) { student.age = [element.stringValue intValue]; } } [self.dataArray addObject:student]; } for (Student *stu in self.dataArray) { NSLog (@"%@ %@ %@ %ld", stu.name, stu.gender, stu.hobby, stu.age); } } JSON之NSJSONSerialization ThirdViewController.m #import "ThirdViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSString *path = [[NSBundle mainBundle]pathForResource:@"JSON_stu" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:path]; /* NSJSONReadingMutableContainers = (1UL << 0) //返回一个数组或者字典 NSJSONReadingMutableLeaves = (1UL << 1) //返回一个字符串 NSJSONReadingAllowFragments = (1UL << 2) //返回一个任意类型对象 */ NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"array ==== %@", array); for (NSDictionary *dic in array) { NSLog(@"dic ==== %@", [dic objectForKey:@"content"]); } } JSON之JSONKit FourViewController.m #import "FourViewController.h" #import "JSONKit.h" /////////////////////////////////////////////////////////注意这里. notice here. @interface FourViewController () @end @implementation FourViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSString *path = [[NSBundle mainBundle]pathForResource:@"JSON_stu" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:path]; // 将JSON数据转化成需要的格式. NSArray *array = [data objectFromJSONData]; NSLog(@"array === %@", array); } JSON之NSJSONSerialization 带tableView 最外层是"[ ]"是数组.然后是" { } "是字典.再是数组,再是字典 FifthViewController.m #import "FifthViewController.h" @interface FifthViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation FifthViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle]pathForResource:@"movie" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; self.dataArray = [NSMutableArray arrayWithArray:array]; NSLog(@"%@",self.dataArray); self.view.backgroundColor = [UIColor cyanColor]; self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.tableView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.tableView]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *array = [self.dataArray[section]objectForKey:@"data"]; return array.count; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataArray.count; } //头标题 (显示 "热门电影 和 热门综艺 "八个字的作用) - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *string = [self.dataArray[section]objectForKey:@"title"]; return string; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; NSDictionary *dic = self.dataArray[indexPath.section]; NSArray *arr = [dic objectForKey:@"data"]; NSDictionary *dic1 = [arr objectAtIndex:indexPath.row]; cell.textLabel.text = [dic1 objectForKey:@"title"]; return cell; } JSON之NSJSONSerialization 带tableView 最外层是" { } "是字典.再是数组,再是字典,与上面的相比少了一层. #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSDictionary *dictionary; @property (nonatomic, strong) NSMutableArray *array; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle]pathForResource:@"MovieList" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; self.dictionary = [NSDictionary dictionaryWithDictionary:dic]; NSLog(@"%@", self.dictionary); //tableView的行数与数组去对应.所以这里写一个数组去接受字典;为了方便设置行数.由于在上面设置属性了,所以下面解析的时候也可以直接通过self调用. self.array = [self.dictionary objectForKey:@"result"]; self.view.backgroundColor = [UIColor cyanColor]; self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.tableView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //tableView. return self.array.count; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; NSDictionary * dic1 = [self.array objectAtIndex:indexPath.row]; cell.textLabel.text = [dic1 objectForKey:@"movieName"]; return cell; }
时间: 2024-11-05 13:38:08