作者:gaoyizhen92 | 来源:互联网 | 2023-09-11 21:31
数据解析:就是按照约定(假象)好的格式提取数据的过程就叫解析;提供数据方(后台):工作就是把数据按照一定的格式存储起来提取数据方(前台):工作就是把数据按照一定的格式读取出来主流的格式
数据解析:就是按照约定(假象)好的格式提取数据的过程就叫解析;
提供数据方(后台):工作就是把数据按照一定的格式存储起来
提取数据方(前台):工作就是把数据按照一定的格式读取出来
主流的格式:XML格式 / JSON格式 ,此两种格式无论前台还是后台都必须熟悉
XML以标签的形式存在,标签由开始标签和结束标签,这一对标签就做节点;
没有父节点的节点叫做根节点,没有子节点的节点就做叶子节点,而我们要使用的数据都在叶子节点中存储;
XML解析的两种工作原理:
1.SAX 基于事件回调(找代理)的解析方法方式,逐行解析,效率低,适合大数据解析,是系统提供的;
2.DOM 把要解析的数据全部读取到内存中,把数据初始化成树状结构,逐层解析,效率高,只适合小数据解析,谷歌提供的第三方解析方法;
—————————————————第一部分SAX解析———————————————————
素材下载Students.xml:
http://pan.baidu.com/s/1hqji1IO
JiSON解析素材下载:
http://pan.baidu.com/s/1mgnefsW
页面基本的搭建不做介绍!
公用model数据模型:
Student.h
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *gender;//性别
@property(nonatomic,copy)NSString *age;//年龄
@property(nonatomic,copy)NSString *says;//座右铭
@property(nonatomic,retain)NSString *position;//属性
@end
Student.m
@implementation Student
-(void)dealloc{
self.name = nil;
self.gender = nil;
self.says = nil;
self.age = nil;
self.position = nil;
[super dealloc];
}
//防止未找到key值所对的value造成Crash
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
—————————————————
两个方法的cell显示一样UITableViewCell
SAXViewController.m
#import "SAXViewController.h"
#import "Student.h"
@interface SAXViewController ()
//存储从xml中解析出来的学生对象
@property(nonatomic,retain)NSMutableArray *dataSource;
@property(nonatomic,retain)Student *student;//存储使用学生对象读取到的信息
@property(nonatomic,retain)NSMutableString *string;//存储读取到节点中的数据
@end
@implementation SAXViewController
- (void)dealloc{
self.dataSource = nil;
self.student = nil;
self.string = nil;
[super dealloc];
}
- (IBAction)handleParser:(UIBarButtonItem *)sender {
//NSXMLParser 系统提供的解析xml文件的类
//第一步:获取文件路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students.xml" ofType:nil];
//第二步:创建解析对象
//创建一个NSData对象
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSXMLParser *parser =[[NSXMLParser alloc]initWithData:data];
//制定代理
parser.delegate = self;
//开始解析
[parser parse];
//释放
[parser release];
}
- (NSMutableArray *)dataSource{
if (_dataSource == nil) {
self.dataSource = [NSMutableArray arrayWithCapacity:0];
}
return [[_dataSource retain]autorelease];
}
#pragma mark xmlPoarserDelegate 代理方法
//开始解析的时候触发这个方法
- (void)parserDidStartDocument:(NSXMLParser *)parser{
// NSLog(@"开始解析了");
//开始解析的时候清空数组中的内容
[self.dataSource removeAllObjects];
}
//读到开始标签的时候开始触发
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//当读取到Student开始标签的时候创建学生对象
if ([elementName isEqualToString:@"Student"]) {
self.student = [[Student alloc]init];
self.student.position = attributeDict[@"position"];
}
//读取到开始标签就初始化字符串self.string
self.string = [NSMutableString stringWithCapacity:0];
}
//读到结束标签的时候开始触发
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"name"]) {
self.student.name = self.string;
}else if([elementName isEqualToString:@"age"]){
self.student.age = self.string;
}else if([elementName isEqualToString:@"gender"]){
self.student.gender = self.string;
}else if ([elementName isEqualToString:@"says"]){
self.student.says = self.string;
}else if ([elementName isEqualToString:@"Student"]){
//把学生对象添加到数组中
[self.dataSource addObject:self.student];
[self.student release];
}
}
//当读到开始标签后的数据触发
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//string 存储的就是读到的数据
//由于系统的解析能力是有限的,如果节点中的内容过长,不能一次性全部读取,此时要把,每次读取到的内容拼接在一起
[self.string appendString:string];
}
//结束解析的时候触发
- (void)parserDidEndDocument:(NSXMLParser *)parser{
// NSLog(@"解析结束了");
//在这里让tableView重新刷新一下ui,重新走一遍数据源方法
[self.tableView reloadData];
}
//返回分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
//显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sax" forIndexPath:indexPath];
//根据cell的下标取出对应的学生
Student *s = self.dataSource[indexPath.row];
cell.textLabel.text = s.name;
return cell;
}
最终效果:
——————————————————————————————————————————————————
————————————————第二部分Dom解析———————————————
两个方法的cell显示一样UITableViewCell
2. GDataXMLNode 是由Google提供的开源的基于C语言的libxml2.dylib动态链接类库封装的OC xml解析类,效率比较高
使用GDataXMLNode 步骤:
1.target -->Build Phases--link Binary 添加libxml2.dylib
2.target -->Build setting 搜 header seach Path 双击后缀添加 /usr/include/libxml2
libxml2.dylib 和 libxml2.2dylib 的区别:前一个是快捷方式,永远指向最新的实体类库,而后一个才是真正的实体类库,使用前一个好处,当系统实体类库更新的时候,老版本的项目就无需再重新导入实体类库;
DOMViewController.m
#import "DOMViewController.h"
#import "GDataXMLNode.h"
#import "Student.h"
@interface DOMViewController ()
@property(nonatomic,retain)NSMutableArray *dataSource;
@end
@implementation DOMViewController
- (void)dealloc{
self.dataSource = nil;
[super dealloc];
}
//懒加载创建数组
- (NSMutableArray *)dataSource{
if (_dataSource == nil) {
self.dataSource = [NSMutableArray arrayWithCapacity:0];
}
return [[_dataSource retain]autorelease];
}
==============RootParser解析=================
- (IBAction)handleRootParser:(UIBarButtonItem *)sender {
//解析开始之前把数组中的元素清空
self.dataSource = nil;
//拿到文件的路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students.xml" ofType:nil];
//将文件内容存储到字符串中
NSString *xmlStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//将要解析的内容写入到GDataXMLDocument 类中
//第二个参数是一个预留参数,就是这个方法的作者,在写这个方法时预留一个参数的位置,留待以后对方的扩充
GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:xmlStr options:0 error:nil];
// GDataXMLElement 节点类
// GDataXMLNode 属性类
//获取document 中所有的根节点
GDataXMLElement *rootElement = [document rootElement];
// NSLog(@"%@",rootElement);//验证
//获取根节点下的所有Student节点
NSArray *stuElements = [rootElement elementsForName:@"Student"];
// NSLog(@"%@",stuElement);//验证
//遍历数组,获取每一个Student节点
for (GDataXMLElement *stuElement in stuElements) {
//获取Student节点下的name/age/gender/says 因为只有一个元素所有firstObject和lastObject一样
GDataXMLElement *nameElement = [[stuElement elementsForName:@"name"]lastObject];
GDataXMLElement *ageElement = [[stuElement elementsForName:@"age"]lastObject];
GDataXMLElement *genderElement = [[stuElement elementsForName:@"gender"]firstObject];
GDataXMLElement *saysElement = [[stuElement elementsForName:@"says"]firstObject];
/*//打印每个节点对象中的数据 验证!
NSLog(@"%@",[nameElement stringValue]);
NSLog(@"%@",[ageElement stringValue]);
NSLog(@"%@",[genderElement stringValue]);
NSLog(@"%@",[saysElement stringValue]);
NSLog(@"________________________");
*/
//创建学生对象,存储节点对象中的数据
Student *stu = [[Student alloc]init];
stu.name = [nameElement stringValue];
stu.age = [ageElement stringValue];
stu.gender = [genderElement stringValue];
stu.says = [saysElement stringValue];
//取出学生节点对象中的属性
GDataXMLNode *node = [stuElement attributeForName:@"position"];
stu.position = [node stringValue];
//将学生对象添加到数组织中
[self.dataSource addObject:stu];
[stu release];
}
[self.tableView reloadData];
// NSLog(@"%@",self.dataSource); //验证
//释放xml文本对象
[document release];
}
RootParser解析效果:
————————————————————————————————————————————
==============xPath相对路径解析=================
- (IBAction)handleXPath:(UIBarButtonItem *)sender {
//清空数组
[self.dataSource removeAllObjects];
//拿到文件的路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students.xml" ofType:nil];
//将文件内容存储到字符串中
NSString *xmlStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//将要解析的内容写入到GDataXMLDocument 类中
//第二个参数是一个预留参数,就是这个方法的作者,在写这个方法时预留一个参数的位置,留待以后对方的扩充
GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:xmlStr options:0 error:nil];
//通过相对路径获取name,age,gender,says 节点
//姓名节点
NSArray *nameElements = [document nodesForXPath:@"//name" error:nil];
//年龄节点
NSArray *ageElements = [document nodesForXPath:@"//age" error:nil];
//性别节点
NSArray *genderElements = [document nodesForXPath:@"//gender" error:nil];
//座右铭节点
NSArray *saysElements = [document nodesForXPath:@"//says" error:nil];
//学生节点
NSArray *studentElements = [document nodesForXPath:@"//Student" error:nil];
//求出学生节点数组中元素的个数
int count = (int)studentElements.count;
for (int i = 0; i
——————————————————————————————————————————————
=======================第三部分JiSON=========================
两个方法的cell显示一样UITableViewCell
JSONViewController.m
#import "JSONViewController.h"
#import "Student.h"
#import "JSONKit.h"
@interface JSONViewController ()
@property(nonatomic,retain)NSMutableArray *dataSource;
@end
@implementation JSONViewController
- (void)dealloc{
self.dataSource = nil;
[super dealloc];
}
//懒加载
- (NSMutableArray *)dataSource{
if (_dataSource == nil) {
self.dataSource = [NSMutableArray arrayWithCapacity:0];
}
return [[_dataSource retain]autorelease];
}
==============系统方法解析=================
- (IBAction)handleSystomParser:(UIBarButtonItem *)sender {
//清空字典内容
self.dataSource = nil;
//1.获取文件路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Student.json" ofType:nil];
//2.将JSON文件读取到Data对象中
NSData *date = [NSData dataWithContentsOfFile:filePath];
// NSJSOnReadingMutableContainers= (1UL <<0), 返回容器是可变容器
// NSJSOnReadingMutableLeaves= (1UL <<1), 返回的找到的数据的是可变的数据
//json的最外层是数组,所以用数组接收
NSMutableArray *dataSource = [NSJSONSerialization JSONObjectWithData:date options:(NSJSONReadingMutableContainers) error:nil];
//数组中都是字典,所以遍历字典
for (NSDictionary *dic in dataSource) {
//创建一个学生对象
Student *stu = [[Student alloc]init];
//kvc赋值
[stu setValuesForKeysWithDictionary:dic];
//加到数组里
[self.dataSource addObject:stu];
//释放
[stu release];
}
//刷新ui
[self.tableView reloadData];
}
系统方法解析效果:
——————————————————————————————————
==============第三方解析=================
- (IBAction)handleThridParer:(UIBarButtonItem *)sender {
//清除数组
self.dataSource = nil;
//获取文件路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Student.json" ofType:nil];
/*系统方法暂时不用(注掉)
NSString *jsOnStr= [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//1.通过json格式的字符串解析json数据 objectFromJSONString返回的数据可以使用可变容器接收也可以使用不可变容器接收
NSArray *dataArray = [jsonStr objectFromJSONString];
NSLog(@"%@",dataArray);
*/
新方法
//2.通过NSDate对象完成解析 objectFromJSONData返回的数据可以使用可变容器接收也可以使用不可变容器接收
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSArray *dataArray2 = [data objectFromJSONData];
// NSLog(@"%@",dataArray2); 验证!
//数组中都是字典,所以遍历字典
for (NSDictionary *dic in dataArray2) {
//创建
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:dic];
[self.dataSource addObject:stu];
[stu release];
}
[self.tableView reloadData];
}
//返回分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
//显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"json" forIndexPath:indexPath];
//取值
Student *s = self.dataSource[indexPath.row];
//显示
cell.textLabel.text = s.says;
return cell;
}
第三方解析效果: