xml文件解析(解析以后在RootTableViewController输出)

这是从美团弄得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
原文地址:https://www.cnblogs.com/fume/p/5320629.html