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