• 4_2网络学习第二天--XML解析


    4_2网络学习第二天--XML解析

     

    1、XML的数据:

     <?xml version="1.0" encoding="utf-8" ?> (表示XML文件版本)

     <CityName>北京</CityName> (一个节点,CityName是结点名,北京结点值)

     <Item key="1" value="A"></Item>  (key="1"是结点属性,key属性名,“1”属性值)

     注意:XML是层次嵌套的结构

    2、解析XML:

    //开始加载数据
    -(void)startDownloadData
    {
        NSString *urlSring=[NSString stringWithFormat:MAIN_SHOP_URL,_city,_category,_offset,_pageSize];
        
        _request=[[LCHttpRequest alloc]init];
        
        [_request requestUrl:urlSring target:self action:@selector(dealDownloadFinish:)];
    
    }
    //加载完成后,解析和处理数据
    -(void)dealDownloadFinish:(LCHttpRequest *)request
    {
        //解析XML -利用第三方开源库 GDataXMLDocument
        GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:request.data options:0 error:nil];
        NSArray *list=[doc nodesForXPath:@"//promotion" error:nil];
        //利用CDataXMLElement获取
        for (GDataXMLElement * promotion in list) {
            
            PromotionModel *model=[[PromotionModel alloc]initWithGDataXMLElement:promotion];
            
            [_allData addObject:model];
        }
    }
    
    #import "PromotionModel.h"
    
    @implementation PromotionModel
    //封住数据模型-利用获得的GDataXMLElement
    -(id)initWithGDataXMLElement:(GDataXMLElement *)element
    {
        if (self=[super init]) {
            self.name=[[element elementsForName:@"name"][0] stringValue];
            self.id=[[element elementsForName:@"id"][0] stringValue];
            self.surl=[[element elementsForName:@"surl"][0] stringValue];
            self.wsdimg=[[element elementsForName:@"wsdimg"][0] stringValue];
            self.durl=[[element elementsForName:@"durl"][0] stringValue];
            self.multipagetitle=[[element elementsForName:@"multipagetitle"][0] stringValue];
            self.price=[[element elementsForName:@"price"][0] stringValue];
            self.priceoff=[[element elementsForName:@"priceoff"][0] stringValue];
            self.currentdealcount=[[element elementsForName:@"currentdealcount"][0] stringValue];
            self.starttime=[[element elementsForName:@"starttime"][0] stringValue];
            self.endtime=[[element elementsForName:@"endtime"][0] stringValue];
            self.sevenrefundallowed=[[element elementsForName:@"sevenrefundallowed"][0] stringValue];
            self.expirerefundallowed=[[element elementsForName:@"expirerefundallowed"][0] stringValue];
            self.district=[[element elementsForName:@"district"][0] stringValue];
            
            self.type2=[[element elementsForName:@"type2"][0] stringValue];
            self.hassub=[[element elementsForName:@"hassub"][0] stringValue];
            self.flag=[[element elementsForName:@"flag"][0] stringValue];
    
        }
        return self;
    }

    3、上拉刷新、下拉加载

    利用第三方开源库是方便的:例如AH3DPullRefresh:

    #pragma mark - 上拉刷新,下拉加载更多
    -(void)addPullRefreahAndPullLoadMore
    {
    
        [_tableView setPullToRefreshHandler:^{
            //从第一行数据开始下载
            _offset=1;
            [self startDownloadData];
        }];
        
        [_tableView setPullToLoadMoreHandler:^{
            _offset+=_pageSize;
            [self startDownloadData];
        }];
    }
    
    //上拉或者下拉会调用下面方法
    //开始加载数据
    -(void)startDownloadData
    {
        NSString *urlSring=[NSString stringWithFormat:MAIN_SHOP_URL,_city,_category,_offset,_pageSize];
        
        _request=[[LCHttpRequest alloc]init];
        
        [_request requestUrl:urlSring target:self action:@selector(dealDownloadFinish:)];
    
    }
    //加载完成后,解析和处理数据
    -(void)dealDownloadFinish:(LCHttpRequest *)request
    {//解析XML
        GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:request.data options:0 error:nil];
        NSArray *list=[doc nodesForXPath:@"//promotion" error:nil];
        
        if (_offset==1) {
            NSLog(@"正在刷新数据");
            [_allData removeAllObjects];
        }
        
        for (GDataXMLElement * promotion in list) {
            
            PromotionModel *model=[[PromotionModel alloc]initWithGDataXMLElement:promotion];
            
            [_allData addObject:model];
        }
        
        //结束刷新
        [_tableView refreshFinished];
        //结束加载更多
        [_tableView loadMoreFinished];
        
        [_tableView reloadData];
    }

     4、多使用第三方开源库实现:

  • 相关阅读:
    2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)
    棋盘问题
    DP待整理
    Monkey and Banana(DP)
    Ignatius and the Princess IV
    【[kuangbin带你飞]专题十二 基础DP1】Max Sum Plus Plus(DP+滚动数组)
    第三届山东ACM Pick apples
    第三届山东省ACM The Best Seat in ACM Contest
    第三届山东ACM省赛 Pixel density
    第三届山东ACM省赛 n a^o7 !
  • 原文地址:https://www.cnblogs.com/muer126/p/4388115.html
Copyright © 2020-2023  润新知