美團xml文件解析

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

#import<UIKit/UIKit.h>

#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<UIKit/UIKit.h>

#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<UIKit/UIKit.h>

@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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,443評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,530評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,407評論 0 375
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,981評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,759評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,204評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,263評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,415評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,955評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,782評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,528評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,222評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,650評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,892評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,675評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,967評論 2 374

推薦閱讀更多精彩內容

  • 概述在iOS開發中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,081評論 3 38
  • 前言 最近忙完項目比較閑,想寫一篇博客來分享一些自學iOS的心得體會,希望對迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,448評論 9 5
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個文章 AppDelegate.h ...
    CC_iOS閱讀 911評論 0 0
  • 哦吼吼,又研究了幾天,把FMDB這個封裝好的數據庫搞定了,寫了個簡單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 541評論 0 0
  • 從二月一直參加老師的課,從情緒的漩渦拔不出來到如今在家也能笑了,從在家根本呆不住到可以一個人在家也享受了,我深切體...
    英寶貝閱讀 700評論 0 0