zoukankan      html  css  js  c++  java
  • XML解析

    //XMLTableViewController.h文件

    #import <UIKit/UIKit.h>

    #import "SecondTableViewController.h"

    @interface XMLTableViewController : UITableViewController<NSXMLParserDelegate>

    //定义显示在字典中的value值

    @property (strong,nonatomic) NSString *str;

    //定义全局数组用于显示所有数据

    @property (strong,nonatomic) NSMutableArray *array;

    //定义字典用于封装集合在的元素

    @property (strong,nonatomic) NSMutableDictionary *dic;

    @end

     //XMLTableViewController.m文件

    #import "XMLTableViewController.h"

    @interface XMLTableViewController ()

    @end

    @implementation XMLTableViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

    //tableView根视图颜色

        self.view.backgroundColor=[UIColor lightGrayColor];

    //tableView标题

        self.title=@"美团城市入住列表";

    //tableView标题颜色

        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor]}];

        //网络请求获取文件

        NSXMLParser *parser=[[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions"]];

    //指定代理

        parser.delegate=self;

    //执行解析

        [parser parse];

        

        

    }

    //开始文件解析

    - (void)parserDidStartDocument:(NSXMLParser *)parser

    {

    //第一次解析XML文件是,初始化集合

        self.array=[NSMutableArray array];

    }

    //结束文件解析

    - (void)parserDidEndDocument:(NSXMLParser *)parser

    {

    }

    //开始文件元素解析

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict

    {

    //    指定特殊节点元素进行操作  

        if ([elementName isEqualToString:@"division"]) {

    //        只有在文档中查找到division节点才初始化字典元素 dic

            self.dic=[NSMutableDictionary dictionary];

            

        }

    }

    //每个节点的类容

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

    {

     //每个节点中的元素内容

        self.str=string;

    }

    //结束文件元素解析

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName

    {

    //    只有节点元素是以下@“”内的字符串时才为dic设置值

        if ([elementName isEqualToString:@"id"]||[elementName isEqualToString:@"name"]||[elementName isEqualToString:@"timezone"]||[elementName isEqualToString:@"timezone_offset_gmt"]||[elementName isEqualToString:@"latitude"]||[elementName isEqualToString:@"longitude"]) {

            [self.dic setObject:self.str forKey:elementName];

        }

    //    当节点是division时,将dic添加到集合中

    //    即  集合中存储了一个字典 

        else if ([elementName isEqualToString:@"division"]) {

            [self.array addObject:self.dic];

        }

    }

      

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return self.array.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *iden=@"reuseIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];

        if (cell==nil) {

            cell=[[UITableViewCell alloc]initWithStyle:3 reuseIdentifier:iden];

        }

        //tableView的正标题

        cell.textLabel.text=self.array[indexPath.row][@"name"];

     //tableView的副标题

        cell.detailTextLabel.text=self.array[indexPath.row][@"id"];

        

        return cell;

    }

    //到下一页

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        SecondTableViewController *secView=[[SecondTableViewController alloc]init];

        secView.strname=self.array[indexPath.row][@"name"];

        secView.strid=self.array[indexPath.row][@"id"];

        secView.strtime=self.array[indexPath.row][@"timezone"];

        secView.strtimeget=self.array[indexPath.row][@"timezone_offset_gmt"];

        secView.strlat=self.array[indexPath.row][@"latitude"];

        secView.strlong=self.array[indexPath.row][@"longitude"];

        [self.navigationController pushViewController:secView animated:YES];

    }

    //  SecondTableViewController.h文件

    #import <UIKit/UIKit.h>

    @interface SecondTableViewController : UITableViewController

    @property (strong,nonatomic) NSMutableArray *arr;

    @property (strong,nonatomic) NSString *strid;

    @property (strong,nonatomic) NSString *strname;

    @property (strong,nonatomic) NSString *strtime;

    @property (strong,nonatomic) NSString *strtimeget;

    @property (strong,nonatomic) NSString *strlat;

    @property (strong,nonatomic) NSString *strlong;

    @end

    //  SecondTableViewController.m文件

    #import "SecondTableViewController.h"

    @interface SecondTableViewController ()

    @end

    @implementation SecondTableViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor=[UIColor lightGrayColor];

        self.title=self.strname;

        self.arr=[NSMutableArray array];

        [self.arr addObject:self.strname];

        [self.arr addObject:self.strid];

        [self.arr addObject:self.strtime];

        [self.arr addObject:self.strtimeget];

        [self.arr addObject:self.strlat];

        [self.arr addObject:self.strlong];

        

        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];

        

    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return self.arr.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

        

        cell.textLabel.text=self.arr[indexPath.row];

        

        return cell;

    }

  • 相关阅读:
    dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
    sqlmap基本命令
    Read-Only Tables 只读表
    Oracle Set操作
    OGG-00664
    Oracle 附加日志(supplemental log)
    Oracle 数字转为字符串 to_char()
    Oracle单引号转义符
    OGG开启DDL,约束信息和索引信息会同步过去吗
    OGG-01161
  • 原文地址:https://www.cnblogs.com/Always-LuoHan/p/5321424.html
Copyright © 2011-2022 走看看