Xml file parsing (output in RootTableViewController after parsing), tableviewcontroller
This is the xml file, region, and latitude and longitude obtained from Meituan.
After you click the region, you can view the longitude and latitude. Because I am lazy and have ready-made text boxes, all of them are lazy.
Below are some boring code.
#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 = @ "City list"; 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"];}/*** start parsing the document ** @ param parser */-(void) parserDidStartDocument :( NSXMLParser *) parser {self. arr = [NSMutableArray array];}/*** parsed ** @ param parser */-(void) parserDidEndDocument :( NSXMLParser *) parser {NSLog (@ "% @", self. arr );} /*** start of file element parsing ** @ param parser the object parsed * @ param elementName the name of the element * @ param namespaceURI namespace * @ param qName * @ param attributeDict attribute dictionary */-(void) parser :( NSXMLParser *) parser attributes :( NSString *) elementName namespaceURI :( nullable NSString *) namespaceURI attributes :( nullable NSString *) qName attributes :( NSDictionary <NSString *, NSString *> *) attributeDict {if ([elementName isinclutostring: @ "division"]) {self. dic1 = [NSMutableDictionary dictionary]; [self. dic1 setDictionary: attributeDict];} /*** end of element resolution in the document ** @ param parser the object parsed ** @ param elementName the name of the element * @ param namespaceURI namespace * @ param qName attribute dictionary */- (void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( nullable NSString *) namespaceURI qualifiedName :( nullable NSString *) qName {if ([elementName isinclutostring: @ "name"] | [elementName isinclutostring: @ "latitude"] | [elementName isinclutostring: @ "longpolling"]) {[self. dic1 setObject: self. str forKey: elementName];} else if ([elementName isinclutostring: @ "division"]) {[self. arr addObject: self. dic1] ;}}/*** parse file element content ** @ param parser resolve object * @ param string displays text content */-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {self. str = string;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // 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 metadata: @ "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] [@ "longpolling"]; [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 = @ "precision"; [self. view addSubview: self. lblname]; self. lblpwd = [[UILabel alloc] initWithFrame: CGRectMake (50,330,100, 50)]; self. lblpwd. text = @ "dimension"; [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: @ "OK" 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