GDataXML是Google開(kāi)發(fā)的一個(gè)XML解析庫(kù),輕便,特點(diǎn)使用非常簡(jiǎn)單灾而,支持XPath。
前言:GDataXML是google基于C語(yǔ)言寫(xiě)的第三方框架扳剿,該源碼文件就一個(gè)類(lèi)旁趟,看其源碼,基本使用了C語(yǔ)言的底層的很多l(xiāng)ib編譯庫(kù)代碼庇绽,所以剛導(dǎo)入使用锡搜,會(huì)報(bào)錯(cuò)提示需要設(shè)置導(dǎo)入需要的鏈接庫(kù)。 另外瞧掺,該第三方框架并沒(méi)有納入Cocoapods耕餐,所以通過(guò)pod搜索不到這個(gè)框架。
使用第三方框架有兩種方式:
1:使用手動(dòng)導(dǎo)包? 下載網(wǎng)址? http://xiazai.jb51.net/201602/yuanma/GDataXML(jb51.net).zip
下載完成導(dǎo)入項(xiàng)目編譯會(huì)出現(xiàn)
第一步:
第二步:
第三步:
這樣編譯一下報(bào)錯(cuò)就會(huì)消失了!!!
手寫(xiě)xml文檔
-----------Hero.h-------------
#import@interface Hero : NSObject
@property(nonnull,strong)NSString *name,*like;
@end
------------ViewController.m---------------
#import "ViewController.h"#import "GDataXMLNode.h"#import "Hero.h"@interface ViewController (){? ? NSMutableDictionary *_dic;? ? UITableView *_table;}@end#define TEST_URL @"http://127.0.0.1/1508ESort.xml"@implementation ViewController- (void)viewDidLoad {? ? [super viewDidLoad];? ? // Do any additional setup after loading the view, typically from a nib.? ? _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];? ? _table.delegate = self;? ? _table.dataSource = self;? ? [self.view addSubview:_table];? ? NSURLSession *session = [NSURLSession sharedSession];? ? NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:TEST_URL] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {? ? ? ? ? ? ? ? GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];? ? ? ? GDataXMLElement *rootEle = [doc rootElement];? ? ? ? _dic = [[NSMutableDictionary alloc]init];? ? ? ? for (GDataXMLElement * sortEle in [rootEle elementsForName:@"sort"]) {? ? ? ? ? ? //準(zhǔn)備字典的 key? 英雄類(lèi)型 ===kind? ? ? ? ? ? //sortEle---NSString *kindStr = [[sortEle attributeForName:@"kind"] stringValue];
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (GDataXMLElement *heroEle in [sortEle elementsForName:@"hort"]) {
Hero *he = [[Hero alloc]init];
he.name = [[[heroEle elementsForName:@"name"] firstObject] stringValue];
he.like = [[[heroEle elementsForName:@"like"] firstObject] stringValue];
[arr addObject:he];
}
//給字典設(shè)置數(shù)值
//以英雄類(lèi)型為key 以該類(lèi)型的英雄數(shù)組為值
[_dic setObject:arr forKey:kindStr];
}
dispatch_async(dispatch_get_main_queue(), ^{
[_table reloadData];
});
}];
[task resume];
}
//表格的分區(qū)數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//字典中的key值得數(shù)量就是表格的分區(qū)數(shù)
return _dic.count;
}
//表格每個(gè)分區(qū)中的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = [_dic.allKeys objectAtIndex:section];
return [[_dic objectForKey:key] count];
}
//創(chuàng)建表格單元格中的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//復(fù)用池機(jī)制
static NSString *cellId = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
//設(shè)置表格的樣式
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
}
//獲取對(duì)應(yīng)的key值,從key值中判斷需要獲取的數(shù)據(jù)來(lái)為單元格賦值
NSString *key = [_dic.allKeys objectAtIndex:indexPath.section];
cell.textLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] like];
return cell;
}
//表格中每個(gè)分區(qū)的標(biāo)題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_dic.allKeys objectAtIndex:section];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end