zoukankan      html  css  js  c++  java
  • 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、多使用第三方开源库实现:

  • 相关阅读:
    kicad 基本操作
    RedHat centos中的上传下推脚本
    使用Node.js爬虫存储MySQL数据库
    解决vue和vue-template-compiler版本不同报错的问题
    Vue组件通信(父子组件通信)-学习笔记
    git 简单操作
    错误解决:redis.exceptions.ResponseError: unknown command 'SENTINEL'
    nginx做泛域名解析的域名的正则判断
    postman对字符串进行base64编码方法和变量的使用
    pymysql报错OperationalError: (2013, 'Lost connection to MySQL server during query')
  • 原文地址:https://www.cnblogs.com/muer126/p/4388115.html
Copyright © 2011-2022 走看看