iOS parsing XML implementation of Provincial urban selection

Source: Internet
Author: User

1, the specific content will not repeat. Look directly at the key code.

ViewController.h

  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, nonatomic) Iboutlet Uipickerview *pickerview; @end

Viewcontroller.m

viewcontroller.m//parsexmltorealizechoosecitydemo////Created by Zhanggui on 15/7/25.//Copyright (c) 2015 Zhan Ggui.    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 ([elementnameisequaltostring:@ "City"]) {Nsmutablearray *temp =[[nsmutablearray alloc] Initwitharray:areaarr];        [City Setvalue:temp Forkey:tempcity];    [Areaarr removeallobjects]; }else if ([elementname isequaltostring:@ "province"]) {nsdictionary *tempdic = [[Nsdictionary alloc] Initwithdictio        Nary: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:[pickervie             W 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:@ "Check results" Message:seletedstr delegate:self cancelbuttontitle:@ "OK    [Otherbuttontitles:nil]; [Aler show];} @end

2. province_data.xml File: Http://pan.baidu.com/s/1c0Cr9ja

3, Source: Https://github.com/ScottZg/GetProvinceCityAreaByXMLFile

4, what questions welcome comments to ask questions.

iOS parsing XML implementation of Provincial urban selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.