iOS--解析
2016-02-24 本文已影响65人
STONEsh
ViewController.m#
//
// ViewController.m
// 解析
//
//
#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Student.h"
#import "JSONKit.h"
@interface ViewController ()<NSXMLParserDelegate>
@property(nonatomic, strong)NSMutableArray *dataArray;
//当前节点
@property(nonatomic, strong)NSString *currentElement;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark ---NSXMLParser---
- (IBAction)xmlParser:(UIButton *)sender {
NSLog(@"xml解析");
//1.获取路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"Stu_xml" ofType:@"txt"];
//2.转换成data对象
NSData *data = [NSData dataWithContentsOfFile:path];
//3.创建解析对象(NSXMLParser是系统自带的xml解析类,基于事件驱动,采用协议回调机制)
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
//4.设置代理
parser.delegate = self;
//5.开始解析
[parser parse];
}
//DOM解析,先读取完整地xml文档,然后在内存中形成树的结构,再根据节点关系,去获取数据,这里我们使用Google提供的第三方类GDataXMLNode来解析,解析的速度比NSXMLParser快.
- (IBAction)domParser:(UIButton *)sender {
NSLog(@"dom解析");
//1.先获取路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"Stu_xml" ofType:@"txt"];
//2.转化成data对象
NSData *data =[NSData dataWithContentsOfFile:path];
//3.创建文档对象,此时文档里存放的是树状结构的数据
//创建error对象
NSError *error ;
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
//4.获取文档节点
GDataXMLElement *rootElement = document.rootElement;
//5.
//初始化数组
self.dataArray = [NSMutableArray arrayWithCapacity:5];
//遍历根节点的子节点(student)
for (GDataXMLElement *subElement in rootElement.children) {
//创建model
Student *student = [Student new];
NSLog(@"-=-=-=-=-=%@",subElement);
//取student基本信息name,gender,age,hobby,phoneNumber
for (GDataXMLElement *contentElement in subElement.children) {
NSLog(@"==========%@",contentElement);
//.name是节点的名字,.stringValue是节点存储的值
/*
if ([contentElement.name isEqualToString:@"name"]) {
student.name = contentElement.stringValue;
} else if ([contentElement.name isEqualToString:@"age"]){
student.age = [contentElement.stringValue intValue];
} else if ([contentElement.name isEqualToString:@"gender"]){
student.gender = contentElement.stringValue;
} else if ([contentElement.name isEqualToString:@"hobby"]){
student.hobby = contentElement.stringValue;
} else if ([contentElement.name isEqualToString:@"phoneNumber"]){
student.phoneNumber = contentElement.stringValue;
}
*/
//或使用KVC
[student setValue:contentElement.stringValue forKey:contentElement.name];
}
//将student对象添加到数组中
[_dataArray addObject:student];
}
for (Student *stu in _dataArray) {
NSLog(@"%@",stu);
}
}
#pragma mark---NSJSONSerialization解析------
- (IBAction)functionParser:(id)sender {
NSLog(@"json解析---------------------------------------系统");
//1.获取路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"Stu_json" ofType:@"txt"];
//2.创建data
NSData *data = [NSData dataWithContentsOfFile:path];
//3.解析jeson数据
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",array);
//创建并初始化数组dataarray
self.dataArray = [[NSMutableArray alloc] initWithCapacity:5];
//遍历数组
for (NSDictionary *dic in array) {
//创建model
Student *stu = [Student new];
[stu setValuesForKeysWithDictionary:dic];
[_dataArray addObject:stu];
}
//检验
for (Student *stu in _dataArray) {
NSLog(@"%@",stu);
}
}
#pragma mark---JSONKit解析----
- (IBAction)jsonKitParser:(UIButton *)sender {
NSLog(@"jsonKit解析");
//1.获取路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"Stu_json" ofType:@"txt"];
//2.创建data
NSData *data = [NSData dataWithContentsOfFile:path];
//3.解析jeson数据
NSArray *array = [data objectFromJSONData];
NSLog(@"%@",array);//数组内最后一个字典大括号后不能有","
//创建并初始化数组dataarray
self.dataArray = [[NSMutableArray alloc] initWithCapacity:5];
//遍历数组
for (NSDictionary *dic in array) {
//创建model
Student *stu = [Student new];
[stu setValuesForKeysWithDictionary:dic];
[_dataArray addObject:stu];
}
//检验
for (Student *stu in _dataArray) {
NSLog(@"%@",stu);
}
}
#pragma mark ---NSXMLParserDelegate---
//1.开始解析文档
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"%s---%d----开始解析文档%d",__FUNCTION__,__LINE__,1);
self.dataArray = [[NSMutableArray alloc] initWithCapacity:5];
}
//2.开始解析标签
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{
NSLog(@"%s---%d----开始解析标签%d",__FUNCTION__,__LINE__,2);
//获取当前节点
_currentElement = elementName;
//这一步作用是根据xml解析出来的节点信息来创建对应的model,(没有赋值过程,只有model的预建立)
if ([elementName isEqualToString:@"student"]) {
Student *student = [Student new];
[_dataArray addObject:student];
}
}
//3.读取标签里的值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"%s---%d----读取标签里的值%d",__FUNCTION__,__LINE__,3);
//方法循环执行,我们需要取数组的最后一个元素
Student *student = _dataArray.lastObject;
//KVC model赋值
[student setValue:string forKey:_currentElement];
NSLog(@"%@",string);
}
//4.结束标签解析
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
NSLog(@"%s---%d----结束标签解析%d",__FUNCTION__,__LINE__,4);
//当前节点置为空
_currentElement = nil;
}
//5.结束解析文档
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"%s---%d----结束解析文档%d",__FUNCTION__,__LINE__,5);
//打印结果,验证解析
for (Student *stu in _dataArray) {
NSLog(@"%@",stu);
}
}
//6.若解析出现错误,打印错误原因
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"%s---%d----%d",__FUNCTION__,__LINE__,6);
NSLog(@"paeserError = %@",parseError);//若出错会打印要解析的文件中,错误的位置
}
@end
Student.h#
//
// Student.h
// 解析
//
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
//姓名
@property(nonatomic, strong)NSString *name;
//性别
@property(nonatomic, strong)NSString *gender;
//爱好
@property(nonatomic, strong)NSString *hobby;
//电话号码
@property(nonatomic, strong)NSString *phoneNumber;
//年龄
@property(nonatomic, assign)int age;
@end
Student.m#
//
// Student.m
// 解析
//
//
#import "Student.h"
@implementation Student
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %d %@", _name,_age,_gender];
}
@end