XML DOM JSON 解析

前端之家收集整理的这篇文章主要介绍了XML DOM JSON 解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#import "MyViewController.h"


@interface MyViewController ()


@end


@implementation MyViewController


- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application,you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/


#pragma mark --SAX解析

// SAX特点: 逐行按每个标签进行解析

// 优点: 适合处理大型数据,不占内存

// 缺点: 不能对某个标签进行修改,不能定位到某个指定的标签

- (IBAction)myButton:(UIButton *)sender

{

// 1.获取文件

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"txt"];

// 2.文件转换成NSData类型

NSData *data = [NSData dataWithContentsOfFile:filePath];

// 3.用系统自带工具NSXMLParser解析数据

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];

// 4.设置代理 签订协议并用代理方法解析

parser.delegate = self;

// 5.开始解析xml

[parser parse];

[parser release];

}



#pragma mark -- 解析电影list

- (IBAction)movieButton:(UIButton *)sender

{

NSString *file = [[NSBundle mainBundle] pathForResource:@"movielist副本" ofType:@"txt"];

NSData *data = [NSData dataWithContentsOfFile:file];

NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSLog(@"dic = %@",dic);

// 取出字典中keyerror_code的值

NSString *error_codeStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"error_code"]];

NSLog(@"error_codeStr = %@",error_codeStr);

NSString *reasonStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"reason"]];

NSLog(@"reasonStr = %@",reasonStr);

if ([error_codeStr isEqualToString:@"0"]) {

NSLog(@"网络请求数据成功");

NSMutableArray *array = [dic objectForKey:@"result"];

NSLog(@"result = %@",array);

} else if ([error_codeStr isEqualToString:@"0001"]) {

NSLog(@"提示用户网络请求失败,请重新加载");

// 弹出提示 用户点确定时 帮助用户重新加载

}

}


#pragma mark --1.开始解析

- (void)parserDidStartDocument:(NSXMLParser *)parser

{

NSLog(@"开始解析");

// 1.初始化数组

self.allStudentArray = [NSMutableArray array];

}


#pragma mark --2.开始解析标签

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

{

NSLog(@"开始标签 = %@",elementName);

// 根据开始标签stu判断是否是学生 如果是,创建一个学生对象

if ([elementName isEqualToString:@"stu"]) {

Students *stu = [[Students alloc] init];

// 将学生对象放入数组中

[self.allStudentArray addObject:stu];

[stu release];

}

self.currentElement = elementName;

}


#pragma mark --3.解析标签

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{


// 取出学生对象,并赋值

Students *stu = [self.allStudentArray lastObject];

/*

// 判断如果当前的开始标签<name>那么我就将值存入

if ([self.currentElement isEqualToString:@"name"]) {

stu.name = string;

}

if ([self.currentElement isEqualToString:@"number"]) {

stu.number = string;

if ([self.currentElement isEqualToString:@"time"]) {

stu.time = string;

if ([self.currentElement isEqualToString:@"address"]) {

stu.address = string;

*/

// 键值编码 根据key 自动匹配属性

// 如果你的key值跟属性名一样,kvc自动属性value赋值

[stu setValue:string forKey:self.currentElement];

}


#pragma mark --4.解析标签结束

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

self.currentElement = nil;

NSLog(@"结束标签 = %@",elementName);

}


#pragma mark --5.解析结束

- (void)parserDidEndDocument:(NSXMLParser *)parser

{

for (Students *stu in self.allStudentArray) {

NSLog(@"name = %@ number = %@ time = %@ address = %@",stu.name,stu.number,stu.time,stu.address);

}

NSLog(@"结束标签");

}






#pragma mark Dom解析

- (IBAction)button1:(UIButton *)sender

{

NSLog(@"Dom解析");

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"txt"];

NSData *data = [NSData dataWithContentsOfFile:filePath];

// 1.创建Dom解析工具对象

GdataxMLDocument *xmlDocument = [[GdataxMLDocument alloc] initWithData:data options:0 error:nil];

// 2.获取根节点

GdataxMLElement *rootElement = xmlDocument.rootElement;

NSLog(@"root = %@",rootElement);

// 6.创建存放model学生的数组

self.allStudentArray = [NSMutableArray array];

// 3.遍历根节点上的子节点

for (GdataxMLElement *subElement in rootElement.children) {

// 5.循环创建存放学生属性值的对象

Students *stu = [[Students alloc] init];

NSLog(@"子节点 = %@",subElement);

// 4.遍历子节点上的子节点

for (GdataxMLElement *ssubElement in subElement.children) {

// 7.kvc进行赋值

[stu setValue:ssubElement.stringValue forKey:ssubElement.name];

}

[self.allStudentArray addObject:stu];

[stu release];


}

// GdataxMLElement *twoElement = [rootElement.children objectAtIndex:1];

// NSLog(@"获取第二个子节点 = %@",twoElement);

for (Students *stu in self.allStudentArray) {

address);

}


}







#pragma mark json解析

- (IBAction)button2:(UIButton *)sender

{

NSLog(@"json");

// 1.找到文件路径

NSString *file = [[NSBundle mainBundle] pathForResource:@"Teachers副本" ofType:@"txt"];

NSData *data = [NSData dataWithContentsOfFile:file];

// 2.通过系统自带json解析工具解析数据

NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSLog(@"array = %@",array);

self.allStudentArray = [NSMutableArray array];

for (NSDictionary *dic in array) {

Students *stu = [[Students alloc] init];

[stu setValuesForKeysWithDictionary:dic];

[self.allStudentArray addObject:stu];

[stu release];

}

for (Students *stu in self.allStudentArray) {

NSLog(@"name = %@ number = %@ gender = %@ phone = %@",stu.gender,stu.phone);

}

// jsonkit工具解析json数据

NSMutableArray *array2 = [data objectFromJSONData];

NSLog(@"array2 = %@",array2);

}






@end

原文链接:https://www.f2er.com/xml/297199.html

猜你在找的XML相关文章