引用GDATA三方文件 !!!
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "ParserTool.h"
#import "Student.h"
#import "DomTool.h"
@interface ViewController ()
- (IBAction)parserAction:(id)sender;
- (IBAction)domAction:(id)sender;
- (IBAction)JSONAction:(id)sender;
@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.
}
- (IBAction)parserAction:(id)sender {
ParserTool *tool = [[ParserTool alloc] init];
[tool startParser];
for (Student *stu in tool.stuArr) {
NSLog(@"%@", stu.name);
}
}
- (IBAction)domAction:(id)sender {
// DomTool *tool = [[DomTool alloc] init];
//
// [tool startDom];
// for (Student *stu in tool.stuArr) {
// NSLog(@"%@~%@", stu.name, stu.sex);
// }
NSMutableArray *arr = [DomTool StartDomTool];
for (Student *stu in arr) {
NSLog(@"%@~%@", stu.name, stu.sex);
}
}
- (IBAction)JSONAction:(id)sender {
// 文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
// 转换成NSData
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic);
}
@end
ParserTool.h
#import <Foundation/Foundation.h>
#import "Student.h"
// 这个是系统提供的xml的解析方法, 通过代理方法完成解析
@interface ParserTool : NSObject<NSXMLParserDelegate>
@property(nonatomic, retain)NSMutableArray *stuArr;
// 用来保存节点中间的字符串内容
@property(nonatomic, copy)NSString *tempStr;
// 开始解析的方法
- (void)startParser;
@end
ParserTool.m
#import "ParserTool.h"
@implementation ParserTool
- (void)startParser {
// 先获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
// 一般都会把数据编程NSData
NSData *data = [NSData dataWithContentsOfFile:path];
// 创建一个xml的解析工具
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 设置工具的代理人
parser.delegate = self;
// 开始解析
[parser parse];
[parser release];
}
#pragma mark 碰到开始的节点就会走这个方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
NSLog(@"%@", elementName);
if ([elementName isEqualToString:@"Student"]) {
// 碰到根节点Student, 先把装对象的数组初始化
self.stuArr = [NSMutableArray array];
} else if ([elementName isEqualToString:@"student"]) {
// 碰到小写的student, 意味着一个新的学生, 需要创建一个新的对象来装学生信息
Student *stu = [[Student alloc] init];
[self.stuArr addObject:stu];
[stu release];
}
}
#pragma mark 找到节点的内容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// 通过属性来保存节点的内容
self.tempStr = string;
}
#pragma mark 当节点结束时会执行这个方法
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// 这个方法里需要对对象进行赋值, 先找到对象
Student *student = [self.stuArr lastObject];
// 给对象进行赋值
// if ([elementName isEqualToString:@"name"]) {
// student.name = self.tempStr;
// } else if ([elementName isEqualToString:@"sex"]) {
// student.sex = self.tempStr;
// } else if ([elementName isEqualToString:@"age"]) {
// student.age = self.tempStr;
// } else if ([elementName isEqualToString:@"hobby"]) {
// student.hobby = self.tempStr;
// }
[student setValue:self.tempStr forKey:elementName];
}
// 这种是系统提供的解析方法, 通过代理的方法逐行解析数据
@end
DomTool.h
#import <Foundation/Foundation.h>
#import "GDataXMLNode.h"
#import "Student.h"
@interface DomTool : NSObject
@property(nonatomic, retain)NSMutableArray *stuArr;
- (void)startDom;
+ (NSMutableArray *)StartDomTool;
@end
DomTool.m
#import "DomTool.h"
@implementation DomTool
- (void)startDom {
// 先获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
// 把路径下的文件变成NSData
NSData *data = [NSData dataWithContentsOfFile:path];
// 初始化数组
self.stuArr = [NSMutableArray array];
// 创建一个解析工具
// 第一个参数: 文件内容, NSData
// 第二个参数: 没有用
// 第三个参数: 错误信息
NSError *error = nil;
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
// 先获取根节点
GDataXMLElement *rootElement = [document rootElement];
// 它可以按照指定的节点名进行筛选, 不需要逐行进行遍历
NSArray *arr = [rootElement elementsForName:@"student"];
// 对满足条件的节点进行遍历
for (NSInteger i = 0; i < arr.count; i++) {
// 找到下标对应的节点
GDataXMLElement *stu = arr[i];
// 创建一个学生
Student *tempStu = [[Student alloc] init];
[self.stuArr addObject:tempStu];
[tempStu release];
GDataXMLElement *name = [[stu elementsForName:@"name"] lastObject];
NSString *stuName = [name stringValue];
tempStu.name = stuName;
GDataXMLElement *sex = [[stu elementsForName:@"sex"] lastObject];
NSString *stuSex = [sex stringValue];
tempStu.sex = stuSex;
}
}
+ (NSMutableArray *)StartDomTool {
// 先获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
// 把路径下的文件变成NSData
NSData *data = [NSData dataWithContentsOfFile:path];
// 初始化数组
NSMutableArray *stuArr = [NSMutableArray array];
// 创建一个解析工具
// 第一个参数: 文件内容, NSData
// 第二个参数: 没有用
// 第三个参数: 错误信息
NSError *error = nil;
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
// 先获取根节点
GDataXMLElement *rootElement = [document rootElement];
// 它可以按照指定的节点名进行筛选, 不需要逐行进行遍历
NSArray *arr = [rootElement elementsForName:@"student"];
// 对满足条件的节点进行遍历
for (NSInteger i = 0; i < arr.count; i++) {
// 找到下标对应的节点
GDataXMLElement *stu = arr[i];
// 创建一个学生
Student *tempStu = [[Student alloc] init];
[stuArr addObject:tempStu];
[tempStu release];
GDataXMLElement *name = [[stu elementsForName:@"name"] lastObject];
NSString *stuName = [name stringValue];
tempStu.name = stuName;
GDataXMLElement *sex = [[stu elementsForName:@"sex"] lastObject];
NSString *stuSex = [sex stringValue];
tempStu.sex = stuSex;
}
return stuArr;
}
@end
Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *age;
@property(nonatomic, copy)NSString *sex;
@property(nonatomic, copy)NSString *hobby;
@end
Student.m
#import "Student.h"
@implementation Student
- (void)dealloc
{
[_name release];
[_age release];
[_sex release];
[_hobby release];
[super dealloc];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end