//
// ViewController.m
// Lesson_15_XML解析
//
// Created by 李洪鹏 on 15/7/20.
// Copyright (c) 2015年 李洪鹏. All rights reserved.
//
#import "ViewController.h"
#import "studentModel.h"
#import "GDataXMLNode.h"
#import "JSONKit.h"
@interface ViewController ()<NSXMLParserDelegate>
//实例变量
{
//用来记录当前操作的标签
NSString *_currentElement;
}
@property (nonatomic, strong)NSMutableArray *xmlSaxArray;
@property (nonatomic, strong)NSMutableArray *xmlDomArray;
@end
@implementation ViewController
//懒加载
//属性才能懒加载 需要重写getter方法
-(NSMutableArray *)xmlDomArray
{
if (_xmlDomArray == nil) {
_xmlDomArray = [NSMutableArray array];
}
return _xmlDomArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)XMLSAXbutton:(UIButton *)sender {
//1.拿到Xml文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"student.xml" ofType:nil];
//2. 根据文件路径获取文件的内容
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
//3.根据data初始化一个NSXMLParser对象
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
//4.设置NSXMLParser对象的相关属性
//是否处理命名空间
[parser setShouldProcessNamespaces:NO];
//是否设置报告命名空间(前缀)
[parser setShouldReportNamespacePrefixes:NO];
//5设置代理
parser.delegate = self;
//6.开始解析了
[parser parse];
}
#pragma mark ------实现NSXMLParser的代理方法
#pragma mark ------打开文档
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//初始化数组
_xmlSaxArray = [NSMutableArray array];
}
#pragma mark------打开标签
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"student"]) {
studentModel *model = [studentModel new];
[_xmlSaxArray addObject:model];
}
_currentElement = elementName;
}
#pragma mark ---取值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
studentModel *model = self.xmlSaxArray.lastObject;
if ([_currentElement isEqualToString:@"name"]) {
model.name = string;
}else if ([_currentElement isEqualToString:@"sex"]){
model.sex = string;
}else if ([_currentElement isEqualToString:@"phone"]){
model.phoneNumber = string;
}
}
#pragma mark---- 关闭标签
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
_currentElement = nil;
}
#pragma mark ---- 关闭文档
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
for (studentModel *model in _xmlSaxArray) {
NSLog(@"%@, %@, %@" , model.name, model.sex, model.phoneNumber);
}
}
//xml DOM解析
- (IBAction)xmlDombutton:(UIButton *)sender {
//1.拿到数据路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"student.xml" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
//3 根据data初始化 GDataXMLDocument对象
GDataXMLDocument *xmlDocumeng = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
//4. 拿到根节点
GDataXMLElement *rootElement = [xmlDocumeng rootElement];
//5. 解析开始
NSArray *studentArray = [rootElement elementsForName:@"student"];
for (GDataXMLElement *element in studentArray) {
//拿到name , sex , phone节点
GDataXMLElement *nameElement = [[element elementsForName:@"name"] firstObject];
GDataXMLElement *sexElement =[[element elementsForName:@"sex"] firstObject];
GDataXMLElement *phoneElement =[[element elementsForName:@"phone"] firstObject];
//创建模型
studentModel *model = [[studentModel alloc] init];
model.name = [nameElement stringValue];
model.sex = [sexElement stringValue];
model.phoneNumber = [phoneElement stringValue];
[self.xmlDomArray addObject:model];
}
for (studentModel *model in self.xmlDomArray) {
NSLog(@"%@, %@, %@", model.name, model.sex, model.phoneNumber);
}
//置为空
self.xmlDomArray = nil;
}
//json系统自带的解析方法
- (IBAction)jsonSystermButton:(UIButton *)sender {
NSString *path = [[NSBundle mainBundle]pathForResource:@"student.json" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
//解析开始
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", array);
for (NSDictionary *dict in array) {
studentModel *model = [[studentModel alloc]init];
[model setValuesForKeysWithDictionary:dict];
NSLog(@"%@, %@, %ld", model.name, model.hobby, model.number);
}
}
//jsonkit第三方
- (IBAction)jsonKitbutton:(UIButton *)sender
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"student.json" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
//解析为数组
NSArray *array = [data objectFromJSONData];
for (NSDictionary *dict in array) {
studentModel *model = [[studentModel alloc] init];
[model setValuesForKeysWithDictionary:dict];
NSLog(@"%@, %@, %ld", model.name, model.hobby, model.number);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end