XML解析及上拉加载下拉刷新
1.XML格式
2.GData和XPath遍历
//配置XML库(配置完才能使用) //(1)添加头文件搜索路径 // Header Search Paths-> /usr/include/libxml2 //(2)添加二进制库 // Link library -> lixml2.dylib //(3)源文件添加编译选项 // -fno-objc-arc //(4)添加头文件 // #import "GDataXMLNode.h"*/
XPath 获取路径 --> 访问数据
NSString *path = [[NSBundle mainBundle] pathForResource:@"xml" ofType:@"txt"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path]; //initWithData -- > 使用NSData初始化,解析 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //获取指定结点 XPath(路径) // CityName路径: /root/systemConfig/CityName NSArray *array = [doc nodesForXPath:@"/root/systemConfig/areaCode" error:nil]; GDataXMLElement *element = [array firstObject]; NSLog(@"name = %@,value = %@",element.name,element.stringValue); NSArray *items = [doc nodesForXPath:@"/root/systemConfig/ComeChannel/Item" error:nil]; GDataXMLElement *item = [items firstObject]; // item.attributes //属性 //获取所有属性 for(GDataXMLElement *attr in item.attributes){ NSLog(@"a-name = %@, a-value = %@",attr.name,attr.stringValue); } //4.获取所有指定名字的结点(不管位置) //XPath NSArray *allItem = [doc nodesForXPath:@"//Item" error:nil]; for(GDataXMLElement *e in allItem){ NSLog(@"name = %@",e.name); } //5.获取所有指定名字的值(不管位置) //XPath NSArray *allValue = [doc nodesForXPath:@"//@value" error:nil]; for(GDataXMLElement *e in allValue){ NSLog(@"value = %@",e.stringValue); } //获取根结点 GDataXMLElement *rootel = doc.rootElement; // NSArray *children = rootel.children; //获取子结点 // rootel.childCount //获取结点个数
3.上拉加载及下拉刷新
一般使用第三方开源库
注:需刷新表格数据
#import "GDataXMLNode.h" #import "AAPullToRefresh.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{ ZJHttpRequest *_request; NSString *_city; NSString *_catagory; // int _offset; //几行 int _pageSize; //#pragma mark - 上拉刷新及下拉加载
-(void)addapullRefreshAndPullLoadMore{ //AAPullToRefresh //top AAPullToRefresh *tv = [_tableView addPullToRefreshPosition:AAPullToRefreshPositionTop actionHandler:^(AAPullToRefresh *v){ _offset = 1; NSLog(@"fresh top"); [v performSelector:@selector(stopIndicatorAnimation) withObject:nil afterDelay:1.0f]; [self startDownloadData]; }]; tv.imageIcon = [UIImage imageNamed:@"launchpad"]; tv.borderColor = [UIColor whiteColor]; //buttom AAPullToRefresh *bv =[_tableView addPullToRefreshPosition:AAPullToRefreshPositionBottom actionHandler:^(AAPullToRefresh *v){ _offset += _pageSize; NSLog(@"add buttom"); [v performSelector:@selector(stopIndicatorAnimation) withObject:nil afterDelay:1.0f]; [self startDownloadData]; }]; bv.imageIcon = [UIImage imageNamed:@"launchpad"]; bv.borderColor = [UIColor whiteColor]; }