iOS解析XML实现省市区选择

1、具体内容就不再赘述了。直接看关键代码。

viewController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
//  ViewController.h
//  ParseXmlToRealizeChooseCityDemo
//
//  Created by zhanggui on 15/7/25.
//  Copyright (c) 2015年 zhanggui. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,NSXMLParserDelegate>
 
- (IBAction)getAction:(UIButton *)sender;
 
@property (weak, nonatomicIBOutlet UIPickerView *pickerView;
@end

  viewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
//  ViewController.m
//  ParseXmlToRealizeChooseCityDemo
//
//  Created by zhanggui on 15/7/25.
//  Copyright (c) 2015年 zhanggui. All rights reserved.
//
 
#import "ViewController.h"
 
@interface ViewController ()
{
    NSMutableDictionary *province,*city;
    NSMutableArray *provinceArr,*cityArr,*areaArr;
    NSString *tempProvince,*tempCity,*tempArea;
 
    NSArray *finalProvinceArr,*finalCityArr,*finalAreaArr;
     
     
    NSString *seletedStr;
}
 
 
 
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
     
    
    NSString *xmlFilePath = [[NSBundle mainBundle]pathForResource:@"province_data" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:xmlFilePath];
    NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data];
    xmlParser.delegate = self;
    province = [NSMutableDictionary new];
    city = [NSMutableDictionary new];
    provinceArr = [NSMutableArray new];
    cityArr = [NSMutableArray new];
    [xmlParser parse];
   
     
    finalProvinceArr = [province allKeys];
     
    NSDictionary *tempCityDic = [province objectForKey:[finalProvinceArr objectAtIndex:0]];
    finalCityArr = [tempCityDic allKeys];
     
 
    finalAreaArr = [tempCityDic objectForKey:[finalCityArr objectAtIndex:0]];
}
#pragma mark - xmlParseDelegateMethod
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"province"]) {
        tempProvince = [attributeDict objectForKey:@"name"];
    }
    if ([elementName isEqualToString:@"city"]) {
        tempCity = [attributeDict objectForKey:@"name"];
        areaArr = [NSMutableArray new];
    }else if ([elementName isEqualToString:@"district"]) {
        [areaArr addObject:[attributeDict objectForKey:@"name"]];
    }
 }
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:@"city"]) {
        NSMutableArray *temp =[[NSMutableArray alloc] initWithArray:areaArr] ;
        [city setValue:temp forKey:tempCity];
        [areaArr removeAllObjects];
    }else if ([elementName isEqualToString:@"province"]) {
        NSDictionary *tempDic = [[NSDictionary alloc] initWithDictionary:city];
        [province setValue:tempDic forKey:tempProvince];
        [city removeAllObjects];
     
    }
}
 
 
#pragma mark - UIPickerViewDeletate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if(component==0) {
        return [finalProvinceArr count];
    }else if (component==1) {
        return [finalCityArr count];
    }else {
        return [finalAreaArr count];
    }
}
 
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component==0) {
        return finalProvinceArr[row];
    }else if (component==1) {
        return finalCityArr[row];
    }else
    {
        return finalAreaArr[row];
    }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component==0) {
        finalCityArr =[[province objectForKey:[finalProvinceArr objectAtIndex:row]] allKeys];
        [pickerView selectRow:0 inComponent:1 animated:NO];
        [pickerView reloadComponent:1];
         
        if ([finalCityArr count]!=0) {
             NSString *selectedProvince = [self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:0] forComponent:0];
             NSString *selectedCity = [self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:1] forComponent:1];
            finalAreaArr = [[province objectForKey:selectedProvince] objectForKey:selectedCity];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            [pickerView reloadComponent:2];
        }
    }else if (component==1) {
        NSString *selectedProvince = [self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:0] forComponent:0];
        NSString *selectedCity = [self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:1] forComponent:1];
        finalAreaArr = [[province objectForKey:selectedProvince] objectForKey:selectedCity] ;
        [pickerView selectRow:0 inComponent:2 animated:NO];
        [pickerView reloadComponent:2];
//        finalAreaArr = [province objectForKey:[province objectForKey:<#(id)#>]];
    }
    NSString *pro =[self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:0] forComponent:0];
    NSString *cit =[self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:1] forComponent:1];
    NSString *are = [self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:2] forComponent:2];
    seletedStr =[NSString stringWithFormat:@"%@%@%@",pro,cit,are];
}
#pragma mark - UIButton Method
- (IBAction)getAction:(UIButton *)sender {
    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"选中结果" message:seletedStr delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [aler show];
}
@end

2、province_data.xml文件下载地址:http://pan.baidu.com/s/1c0Cr9ja

3、源码下载地址:https://github.com/ScottZg/GetProvinceCityAreaByXMLFile

4、有什么疑问欢迎评论提问。

上一篇:kubernetes(2):Centos7 安装 kubernetes 1.5 版本


下一篇:线程,进程 区别