XML解析
#import "RootViewController.h"
#import "StudentModel.h"
@interface RootViewController ()<NSXMLParserDelegate>
@property(nonatomic,strong)UIButton *saxbutton;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableString *myString;
@end
@implementation RootViewController
// 解析
// iOS常見的兩種數(shù)據(jù)格式 : XML 和 JSON
// XML 解析方法 1. (系統(tǒng)方法)SAX解析 2. (google) GData方法
// JSON 1. 系統(tǒng)方法 2. JSONKit
- (void)viewDidLoad {
[super viewDidLoad];
self.saxbutton = [UIButton buttonWithType:UIButtonTypeSystem];
self.saxbutton.frame = CGRectMake(100, 100, 100, 100);
self.saxbutton.backgroundColor = [UIColor blueColor];
[self.saxbutton setTitle:@"sax解析" forState:UIControlStateNormal];
self.saxbutton.tintColor = [UIColor redColor];
[self.view addSubview:self.saxbutton];
[self.saxbutton addTarget:self action:@selector(saxbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saxbuttonAction:(UIButton *)sender{
[self p_data];
}
// 處理數(shù)據(jù)的私有方法
- (void)p_data{
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"txt"];
// 先把數(shù)據(jù)從文件中讀出來
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(@"%@",data);
// 1. 系統(tǒng)解析類
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data];
// 2. 設(shè)置代理
xmlParser.delegate = self;
// 3. 開始解析
[xmlParser parse];
}
#pragma mark 代理方法
// 1. 開始解析文檔
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"開始解析文件");
self.dataArr = [NSMutableArray array];
}
// 2. 開始解析標(biāo)簽
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"student"]) {
StudentModel *s = [[StudentModel alloc]init];
// 判斷標(biāo)簽名,如果是的話,就放到數(shù)組里面
[self.dataArr addObject:s];
}
}
// 3. 解析內(nèi)容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// 把內(nèi)容拿到自己的字符串中
self.myString = [NSMutableString string];
[self.myString appendString:string];
}
// 4. 結(jié)束解析標(biāo)簽
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
StudentModel *s = [self.dataArr lastObject];
[s setValue:self.myString forKey:elementName];
}
// 5. 結(jié)束解析文檔
- (void)parserDidEndDocument:(NSXMLParser *)parser{
for (StudentModel *s in self.dataArr) {
NSLog(@"name:%@ gender:%@ hobby:%@ word:%@",s.name,s.gender,s.hobby,s.word);
}
}
XML_GData解析
#import "RootViewController.h"
#import "Person.h"
#import "GDataXMLNode.h"
@interface RootViewController ()
@property(nonatomic,strong)UIButton *domButton;
@property(nonatomic,strong)NSMutableArray *dataArr;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.domButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.domButton.frame = CGRectMake(100, 100, 100, 100);
[self.domButton setTitle:@"dom解析" forState:UIControlStateNormal];
[self.view addSubview:self.domButton];
[self.domButton addTarget:self action:@selector(domButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view.
}
- (void)domButtonAction:(UIButton *)sender{
[self p_Gdate];
}
// 這里我們使用谷歌提供好的第三方,進(jìn)行XML解析,這個第三方通過很好的封裝,解析更穩(wěn)定,更快
- (void)p_Gdate{
NSLog(@"GDate解析");
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
self.dataArr = [NSMutableArray array];
// 1. 初始化一個XMLDocument
GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
// 2. 生成根節(jié)點
GDataXMLElement *rootElement = xmlDocument.rootElement;
// 通過根標(biāo)簽 拿到子標(biāo)簽
for (GDataXMLElement *childElement in rootElement.children) {
Person *person = [[Person alloc]init];
// stringvalue的屬性可以拿到標(biāo)簽下面的值
// name的屬性,可以拿到標(biāo)簽的名字
for (GDataXMLElement *valueElement in childElement.children) {
[person setValue: valueElement.stringValue forKey:valueElement.name];
}
[self.dataArr addObject:person];
}
for (Person *per in self.dataArr) {
NSLog(@"name:%@ gender:%@ hobby: %@ word: %@",per.name,per.gender,per.hobby,per.word);
}
}
JSON解析
#import "RootViewController.h"
#import "Student.h"
#import "JSONKit.h"
@interface RootViewController ()
@property(nonatomic,strong)UIButton *jsonButton;
@property(nonatomic,strong)NSMutableArray *dataArr;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.jsonButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.jsonButton.frame = CGRectMake(100, 100, 100, 100);
[self.jsonButton setTitle:@"JSON解析" forState:UIControlStateNormal];
[self.view addSubview:self.jsonButton];
[self.jsonButton addTarget:self action:@selector(jsonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view.
}
- (void)jsonButtonAction:(UIButton *)sender{
[self p_JSONdate];
}
- (void)p_JSONdate{
// 使用系統(tǒng)方法
/*
NSLog(@"JSON解析");
// 1. 獲取文件路徑
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data_json" ofType:@"txt"];
// 2. 保存data
NSData *data = [NSData dataWithContentsOfFile:filePath];
// 3. 使用系統(tǒng)的方法進(jìn)行json解析
NSArray *tempArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
self.dataArr = [NSMutableArray array];
// 4. forin 利用KVC進(jìn)行賦值
for (NSDictionary *d in tempArr) {
Student *s = [[Student alloc]init];
[s setValuesForKeysWithDictionary:d];
[self.dataArr addObject:s];
}
for (Student *s in self.dataArr) {
NSLog(@"name: %@ gender: %@ hobby: %@ word: %@",s.name,s.gender,s.hobby,s.word);
}
*/
// 使用第三方方法 JSONKit
// 1. 獲取文件路徑
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data_json" ofType:@"txt"];
// 2. 保存data
NSData *data = [NSData dataWithContentsOfFile:filePath];
self.dataArr = [NSMutableArray array];
self.dataArr = [data objectFromJSONData];
for (NSDictionary *s in self.dataArr) {
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:s];
NSLog(@"name:%@ gender: %@ hobby: %@ word: %@",stu.name,stu.gender,stu.hobby,stu.word);
}
}