美团xml文件解析

网址:http://www.meituan.com/api/v1/divisions

#import

#import "ViewController.h"

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

#import

#import "SecondTableViewController.h"

@interface ViewController : UIViewController

/**

*  全局的集合 用来添加字典的

*/

@property(strong,nonatomic) NSMutableArray *arrM;

/**

*  字典显示 元素名称和value值

*/

@property(strong,nonatomic) NSMutableDictionary *dicM;

/**

*  str 实际上是 字典中的value值

*/

@property(strong,nonatomic) NSString *str;

@property(strong,nonatomic) UITableView *tableview;

/**

*  城市名字

*/

@property(strong,nonatomic) NSMutableArray *arrMname;

/**

*  城市拼音

*/

@property(strong,nonatomic) NSMutableArray *arrMid;

/**

*  城市维度

*/

@property(strong,nonatomic) NSMutableArray *arrMlatitude;

/**

*  城市经度

*/

@property(strong,nonatomic) NSMutableArray *arrMlongitude;

/**

*  存储集合的字典

*/

@property(strong,nonatomic) NSDictionary *dictotal;

@end

#import "ViewController.h"@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

  [super viewDidLoad];

  //指定xml文件路径 

  NSURL *url = [NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions"];    //为 parser 指定初始化

  NSXMLParser *pareser = [[NSXMLParser alloc] initWithContentsOfURL:url];  

//指定代理  

pareser.delegate = self; 

  //实现文件xml解析 执行代理方法 

  BOOL bol = [pareser parse];  

//=返回解析的结果 成功 或 失败 

  NSLog(@"%d",bol); 

  //初始化集合   

self.arrMname = [NSMutableArray array]; 

  //初始化tableview  

self.tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; 

  self.tableview.backgroundColor = [UIColor colorWithRed:1.000 green:0.942 blue:0.820 alpha:1.000]; 

  //指定代理 

  self.tableview.dataSource = self; 

  self.tableview.delegate = self;  

//添加到父视图  

[self.view addSubview:self.tableview];   

//显示标题    self.title = @"美团城市";   

//设置导航栏上面的右边按钮   

UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(next)];    self.navigationItem.rightBarButtonItem = rightbutton;   

//设置导航栏的前景色   

[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.876 green:0.399 blue:0.225 alpha:1.000]];  

//设置导航栏标题的字体大小和字体颜色   

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1.000 green:0.350 blue:0.427 alpha:1.000],NSForegroundColorAttributeName,[UIFont fontWithName:@"Arial-Bold" size:30],NSFontAttributeName, nil]];  

}

//跳转到下一页

-(void)next

{   

SecondTableViewController *second = [[SecondTableViewController alloc] initWithStyle:UITableViewStylePlain];   

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

}

/** *  文档解析开始 初始化全局的集合

* * @param parser

*/

-(void)parserDidStartDocument:(NSXMLParser *)parser

{   

self.arrM = [NSMutableArray array];

}

/** *  文档解析结束

* *  @param parser

*/

-(void)parserDidEndDocument:(NSXMLParser *)parser

{   

//输出集合内容   

//NSLog(@"%@",self.arrM);

}

/** *  文档元素 解析 开始

* *  @param parser        解析的对象

*  @param elementName  元素的名称

*  @param namespaceURI  命名空间

*  @param qName

*  @param attributeDict 属性的字典

*/

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

{

//找到文档中User元素,开始初始化字典

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

//初始化字典

self.dicM = [NSMutableDictionary dictionary];

//向字典中添加属性元素

[self.dicM setDictionary:attributeDict];

}

}

/**

*  文档中解析结束

*

*  @param parser

*  @param elementName  元素名称

*  @param namespaceURI

*  @param qName

*/

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

{

//判断元素的关键字

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

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

}

//元素标签时才向集合中添加字典

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

[self.arrM addObject:self.dicM];

}

}

/**

*  解析文件内容

*

*  @param parser 元素对象

*  @param string 显示文本内容

*/

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

{

self.str = string;

}

//显示分区数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

//每一个分区显示的行数

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

{

return self.arrM.count;

}

//显示每一行的内容

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

{

static NSString *iden = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];

if (cell == nil) {

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

}

//初始化集合

self.arrMid = [NSMutableArray array];

self.arrMlatitude = [NSMutableArray array];

self.arrMlongitude = [NSMutableArray array];

//初始化字典

self.dictotal = [NSDictionary dictionary];

for (_dictotal in self.arrM) {

//将关键字对应的value值添加到集合中

[self.arrMname addObject:_dictotal[@"name"]];

[self.arrMid addObject:_dictotal[@"id"]];

[self.arrMlatitude addObject:_dictotal[@"latitude"]];

[self.arrMlongitude addObject:_dictotal[@"longitude"]];

}

//显示信息

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

cell.detailTextLabel.text = self.arrMid[indexPath.row];

return cell;

}

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

{

//初始化类的对象

SecondTableViewController *second = [[SecondTableViewController alloc] initWithStyle:UITableViewStylePlain];

//获取点击的索引值

second.index = indexPath.row;

//将集合赋给下一页要显示的集合

second.arrMname = self.arrMname;

second.arrMlatitude = self.arrMlatitude;

second.arrMlongitude = self.arrMlongitude;

//跳转到下一页 (导航视图)

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

}

//显示行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 60;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

#import

@interface SecondTableViewController : UITableViewController

/**

*  城市名集合

*/

@property(strong,nonatomic) NSMutableArray *arrMname;

/**

*  相应城市的维度的集合

*/

@property(strong,nonatomic) NSMutableArray *arrMlatitude;

/**

*  相应城市经度的集合

*/

@property(strong,nonatomic) NSMutableArray *arrMlongitude;

/**

*  从第一页接收信息的集合

*/

@property(strong,nonatomic) NSMutableArray *arrRevice;

/**

*  获取索引值

*/

@property(assign,nonatomic) NSInteger index;

@end

#import "SecondTableViewController.h"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

//添加背景色

self.view.backgroundColor = [UIColor colorWithRed:0.909 green:1.000 blue:0.482 alpha:1.000];

//设置导航栏的左按钮

UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];

self.navigationItem.leftBarButtonItem = leftbutton;

self.title = @"纬度与经度";

//唯一标识的重用

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

}

/**

*  返回上一页

*/

-(void)back

{

//跳转到上一页

[self.navigationController popToRootViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

//显示的分区数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie

{

return 3;

}

//每个分区显示的行数

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

return 1;

}

//显示每行的内容

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

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

if (indexPath.section == 0) {

cell.textLabel.text = self.arrMname[self.index];

}

else if(indexPath.section == 1) {

cell.textLabel.text = self.arrMlatitude[self.index];//纬度

}else

{

cell.textLabel.text = self.arrMlongitude[self.index];//经度

}

return cell;

}

//显示行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 60;

}

//显示头部标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

if (section == 0) {

return @"显示纬度与经度";

}

return @"";

}

/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the specified item to be editable.

return YES;

}

*/

/*

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete the row from the data source

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

} else if (editingStyle == UITableViewCellEditingStyleInsert) {

// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

}

}

*/

/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

}

*/

/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the item to be re-orderable.

return YES;

}

*/

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

美团xml文件解析_第1张图片
美团xml文件解析_第2张图片

你可能感兴趣的:(美团xml文件解析)