iOS UIXMLParser 解析数据

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

//导入UItableview 代理方法

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

//定义一个可变集合来接收数据源

@property (nonatomic ,strong) NSMutableArray *arrCity;

//定义可变字典来添加集合元素

@property (nonatomic ,strong) NSMutableDictionary *dicCity;

//创建一个全局的字符串来传值

@property (nonatomic ,strong) NSString *str;

//创建tableview来显示数据

@property (nonatomic ,strong) UITableView *tableView;

@end

#import "ViewController.h"

@interface ViewController ()<NSXMLParserDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //设置网络请求路径

    NSXMLParser *parser= [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im73djcx"]];

         parser.delegate = self;

    BOOL bol = [parser parse];

    NSLog(@"bol = is %d",bol);

     //初始化tableview

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];

    //指定代理

      self.tableView.delegate = self;

    self.tableView.dataSource =self;

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

    //显示tableview

    [self.view addSubview:self.tableView];

}

//开始解析xml

- (void)parserDidStartDocument:(NSXMLParser *)parser{

    //初始化可变集合self.arrCity

    self.arrCity = [NSMutableArray array];

}

//开始解析元素

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

    //找到文档中的dicision元素, 开始出事化字典

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

            self.dicCity = [NSMutableDictionary dictionary];

    }

}

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

    //判断元素的关键字,只有符合 id,name 的时候才向字典中添加元素

    if ([elementName isEqualToString:@"id"]||[elementName isEqualToString:@"name"]) {

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

    }

    //判断元素的关键字,只有符合 longitude,latitude 的时候才向字典中添加元素

   if ([elementName isEqualToString:@"latitude"]||[elementName isEqualToString:@"longitude"]){

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

    }

    //直到找到division元素时才向arrcity中添加字典dic

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

            [self.arrCity addObject:self.dicCity];

    }    

}

//解析文件元素内容,显示内容

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

    self.str = string;

    

}

//数据源 方法 显示行数

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

{

    return self.arrCity.count;

    

  

}

// 数据源  显示信息到cell上

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

    static NSString *iden = @"Cell";

    //定义个UITableViewCell *cell对象 指定唯一标识

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];

    //判断单元格cell是否存在

    if (cell ==nil)

    {   //如果不存在 重新创建一个cell

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

    }

    //解析集合为一个字典

    NSDictionary *dic = self.arrCity[indexPath.row];

    //指定副标题

    cell.detailTextLabel.text = dic[@"id"];

    //添加内容

    cell.textLabel.text = dic[@"name"];

    return cell;

}

//代理方法

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

    //创建一个SecondViewController实例对象second,

    SecondViewController *second = [[SecondViewController alloc] init];

    //利用属性str 传值到第二页 让第二页的lable显示对应城市的经度纬度

    second.str = [NSString stringWithFormat:@"%@城市: 经度为:%@ 维度为:%@",self.arrCity[indexPath.row][@"name"],self.arrCity[indexPath.row][@"longitude"],self.arrCity[indexPath.row][@"latitude"]];

    //压栈到diery

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

    

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//定义的lable 用来显示经度纬度

@property (nonatomic ,strong) UILabel *lable;

//*str 用来传值

@property (nonatomic ,strong) NSString *str;

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImageView *imgV = [[UIImageView alloc] initWithFrame:self.view.frame];

    imgV.image =[UIImage imageNamed:@"62R58PICJdE_1024.jpg"] ;

    self.view.backgroundColor = [UIColor colorWithPatternImage:imgV.image];

    //初始化label

   self.lable = [[UILabel alloc] initWithFrame:CGRectMake(100,100,260,150)];

    //设置背景颜色

    self.lable.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"82r58PICYeM_1024.jpg"]];

    self.lable.numberOfLines = 10;

    self.lable.text = self.str;

  

    //把lable显示在view上

    [self.view addSubview: self.lable];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 运行结果如下:

原文地址:https://www.cnblogs.com/liumu/p/5321088.html