xml檔案解析(解析以後在RootTableViewController輸出),tableviewcontroller

來源:互聯網
上載者:User

xml檔案解析(解析以後在RootTableViewController輸出),tableviewcontroller

 

 

這是從美團弄得xml檔案,地區和經緯度。

你點了地區以後 ,  就可以查看經緯度 ,因為筆者懶, 有現成的文字框 , 所有偷懶了。

 

下面是一些枯燥的代碼了 。

 

#import <UIKit/UIKit.h>#import "RootTableViewController.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 {    self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStyleGrouped]];    return YES;}

 

#import <UIKit/UIKit.h>#import "SecondViewController.h"@interface RootTableViewController : UITableViewController<NSXMLParserDelegate>@property(strong,nonatomic)NSMutableArray *arr;@property(strong,nonatomic)NSMutableDictionary *dic1;@property(strong,nonatomic)NSString *str;@property(strong,nonatomic)NSMutableArray *arrname;@end

 

#import "RootTableViewController.h"@interface RootTableViewController ()@end@implementation RootTableViewController- (void)viewDidLoad {    [super viewDidLoad];    self.title=@"城市列表";    self.arrname=[NSMutableArray array];    NSURL *url=[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im7coqq1"];    NSData *data=[NSData dataWithContentsOfURL:url];    NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data];    parser.delegate=self;    BOOL bol=[parser parse];    NSLog(@"%d",bol);    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];}/** *  文檔開始解析 * *  @param parser parser */- (void)parserDidStartDocument:(NSXMLParser *)parser{    self.arr=[NSMutableArray array];}/** *  解析完畢 * *  @param parser parser */- (void)parserDidEndDocument:(NSXMLParser *)parser{    NSLog(@"%@",self.arr);}/** *  文件項目  解析開始 * *  @param parser        解析的對象 *  @param elementName   元素的名稱 *  @param namespaceURI  命名空間 *  @param qName *  @param attributeDict 屬性的字典 */-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{                if ([elementName isEqualToString:@"division"]) {        self.dic1=[NSMutableDictionary dictionary];        [self.dic1 setDictionary:attributeDict];            }}/** *  文檔中元素 解析結束 * *  @param parser       解析的對象 *  @param elementName  元素的名稱 *  @param namespaceURI 命名空間 *  @param qName        屬性的字典 */- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{    if ([elementName isEqualToString:@"name"]||[elementName isEqualToString:@"latitude"]||[elementName isEqualToString:@"longitude"]) {        [self.dic1 setObject:self.str forKey:elementName];    }    else if ([elementName isEqualToString:@"division"])    {        [self.arr addObject:self.dic1];    }}/** *  解析檔案元素的內容 * *  @param parser 解析對象 *  @param string 顯示的常值內容 */- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{    self.str=string;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#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];        for (NSDictionary *dic in self.arr) {        [self.arrname addObject:dic[@"name"]];    }        cell.textLabel.text=self.arrname[indexPath.row];    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%@",self.arr[indexPath.row]);    SecondViewController *secondvc=[[SecondViewController alloc] init];    secondvc.strweidu=self.arr[indexPath.row][@"latitude"];    secondvc.strjindu=self.arr[indexPath.row][@"longitude"];     [self presentViewController:secondvc animated:YES completion:nil];}@end

 

#import "ViewController.h"#import "RootTableViewController.h"@interface SecondViewController : ViewController@property(strong,nonatomic)UILabel *lblname;@property(strong,nonatomic)UILabel *lblpwd;@property(strong,nonatomic)UITextField *name;@property(strong,nonatomic)UITextField  *pwd;@property(strong,nonatomic)UIButton *buton;@property(strong,nonatomic)NSString *strweidu;@property(strong,nonatomic)NSString *strjindu;@end

 

#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {    [super viewDidLoad];    UIImageView *image=[[UIImageView alloc]initWithFrame:self.view.frame ];    [image setImage:[UIImage imageNamed:@"15EADA084F41FF349CED23058FD34D0E"]];    [self.view addSubview:image];    self.lblname=[[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 50)];    self.lblname.text=@"精度";    [self.view addSubview:self.lblname];    self.lblpwd=[[UILabel alloc]initWithFrame:CGRectMake(50, 330, 100, 50)];    self.lblpwd.text=@"維度";        [self.view addSubview:self.lblpwd];    self.name=[[UITextField alloc] initWithFrame:CGRectMake(150, 200, 200, 50)];    [self.view addSubview:self.name];           self.name.text =self.strweidu;                    self.name.borderStyle=UITextBorderStyleRoundedRect;    self.pwd=[[UITextField alloc] initWithFrame:CGRectMake(150, 330, 200, 50)];    [self.view addSubview:self.pwd];    self.pwd.text=self.strjindu;    self.pwd.borderStyle=UITextBorderStyleRoundedRect;    self.buton=[[UIButton alloc] initWithFrame:CGRectMake(120, 400, 174, 66)];    self.buton.backgroundColor=[UIColor colorWithRed:0.532 green:1.000 blue:0.161 alpha:1.000];    self.buton.layer.cornerRadius=10;    [self.buton setTitle:@"確認" forState:0];    [self.buton setTitleColor:[UIColor whiteColor] forState:0];    [self.buton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:self.buton];}-(void)next{    RootTableViewController *root=[[RootTableViewController alloc]init];                [self presentViewController:root animated:YES completion:nil];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.