美團xml文件解析

網(wǎng)址: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;

/**

*? 城市經(jīng)度

*/

@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;?

? //實現(xiàn)文件xml解析 執(zhí)行代理方法?

? 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

{? ?

//輸出集合內(nèi)容? ?

//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];

}

}

/**

*? 解析文件內(nèi)容

*

*? @param parser 元素對象

*? @param string 顯示文本內(nèi)容

*/

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

{

self.str = string;

}

//顯示分區(qū)數(shù)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

//每一個分區(qū)顯示的行數(shù)

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

{

return self.arrM.count;

}

//顯示每一行的內(nèi)容

-(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;

/**

*? 相應城市經(jīng)度的集合

*/

@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 = @"緯度與經(jīng)度";

//唯一標識的重用

[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

//顯示的分區(qū)數(shù)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie

{

return 3;

}

//每個分區(qū)顯示的行數(shù)

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

return 1;

}

//顯示每行的內(nèi)容

- (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];//經(jīng)度

}

return cell;

}

//顯示行高

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

{

return 60;

}

//顯示頭部標題

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

{

if (section == 0) {

return @"顯示緯度與經(jīng)度";

}

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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子徐鹤,更是在濱河造成了極大的恐慌悯蝉,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蝴韭,居然都是意外死亡烤惊,警方通過查閱死者的電腦和手機乔煞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來柒室,“玉大人渡贾,你說我怎么就攤上這事⌒塾遥” “怎么了空骚?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長擂仍。 經(jīng)常有香客問我囤屹,道長,這世上最難降的妖魔是什么逢渔? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任肋坚,我火速辦了婚禮,結果婚禮上肃廓,老公的妹妹穿的比我還像新娘智厌。我一直安慰自己,他們只是感情好盲赊,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布铣鹏。 她就那樣靜靜地躺著,像睡著了一般哀蘑。 火紅的嫁衣襯著肌膚如雪诚卸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天绘迁,我揣著相機與錄音惨险,去河邊找鬼。 笑死脊髓,一個胖子當著我的面吹牛辫愉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播将硝,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恭朗,長吁一口氣:“原來是場噩夢啊……” “哼屏镊!你這毒婦竟也來了?” 一聲冷哼從身側響起痰腮,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤而芥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后膀值,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棍丐,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年沧踏,在試婚紗的時候發(fā)現(xiàn)自己被綠了歌逢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡翘狱,死狀恐怖秘案,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情潦匈,我是刑警寧澤阱高,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站茬缩,受9級特大地震影響赤惊,放射性物質發(fā)生泄漏。R本人自食惡果不足惜凰锡,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一荐捻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寡夹,春花似錦、人聲如沸厂置。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昵济。三九已至智绸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間访忿,已是汗流浹背瞧栗。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留海铆,地道東北人迹恐。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像卧斟,于是被迫代替她去往敵國和親殴边。 傳聞我的和親對象是個殘疾皇子憎茂,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

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