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
xml文件解析(解析以后在RootTableViewController输出)的更多相关文章
- 类的反射及xml文件的解析
类的反射 xml文件的解析 .properties||.xml配置文件的创建及读取内容 //创建对象 Properties properties = new Properties(); //存储 pr ...
- JAVA读取XML文件并解析获取元素、属性值、子元素信息
JAVA读取XML文件并解析获取元素.属性值.子元素信息 关键字 XML读取 InputStream DocumentBuilderFactory Element Node 前言 最 ...
- 用SAX和PULL进行XML文件的解析与生成
XML解析有传统的dom方法还有Jsoup,SAX,PULL等,这里讲的是比较省内存的SAX和PULL方法.Android中极力推荐用PULL的方式来解析,我个人觉得pull确实比较简单,但其内部的逻 ...
- JDOM方法实现对XML文件的解析
首先要下载JDOM.jar包,下载地址:http://download.csdn.net/detail/ww6055/8880371 下载到JDOM.jar包之后导入到工程中去. 实例程序: book ...
- xml文件的解析
1. xml文件的解析 void CDataMgr::readStringData() { std::string xml_name = "config/string.xml"; ...
- [置顶] Android开发之XML文件的解析
Android系统开发之XML文件的解析 我们知道Http在网络传输中的数据组织方式有三种分别为:XML方式.HTML方式.JSON方式.其中XML为可扩展标记语言,如下: <?xml vers ...
- 【文件处理】xml 文件 DOM解析
一.Java解析xml.解析xml四种方法.DOM.SAX.JDOM.DOM4j.XPath 此文针对其中的DOM方法具体展开介绍及代码分析 sax.dom是两种对xml文档进行解析的方法(没有具体实 ...
- Java中使用DOM4J来生成xml文件和解析xml文件
一.前言 现在有不少需求,是需要我们解析xml文件中的数据,然后导入到数据库中,当然解析xml文件也有好多种方法,小编觉得还是DOM4J用的最多最广泛也最好理解的吧.小编也是最近需求里遇到了,就来整理 ...
- 【Android】XML文件的解析
1.首先我们可以在res包路径下创建一个raw包,然后在raw下创建一个email.xml 文件,并修改其内容如下: <?xml version="1.0" encoding ...
- xml文件以及解析
1.创建一个xml文件 <?xml version="1.0" encoding="UTF-8"?> <!-- xml:是一个可扩展的标记语言 ...
随机推荐
- 如何利用mono把.net windows service程序迁移到linux上
How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...
- 【腾讯Bugly干货分享】一步一步实现Android的MVP框架
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5799d7844bef22a823b3ad44 内容大纲: Android 开发 ...
- 【腾讯Bugly干货分享】程序员们也该知道的事——“期权和股票”
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/pfj9NLLuKYAfJJF84R9WAw 作者:B ...
- NodeJs 开发微信公众号(四)微信网页授权
微信的网页授权指的是在微信公众号中访问第三方网页时获取用户地理.个人等信息的权限.对于开发了自己的网页app应用时,获取个人的信息非常重要.上篇博客讲到了注册时可以获取用户的信息,很多人会问为什么还需 ...
- Spring8:一些常用的Spring Bean扩展接口
前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...
- 轻量级的日期插件--datebox
jquery的日期插件有好几款,H5中的input也可以自带日期选择.但为什么要再写一个,有两个理由,一个是引用的文件太大,而有时候只需要很简单的功能,二个是想加一些自定义的效果不好改. 我写的这个功 ...
- Linux下UPnP sample分析
一.UPnP简介 UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...
- 关于js中的this之判断this
this绑定规则的优先级顺序 new操作符绑定 > 显示绑定 > 隐式绑定 > 默认绑定 所以在判断函数在某个调用位置应用的是哪条规则,可以按下列这样的顺序 if(函数在ne ...
- [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具
==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...
- Ios生产证书申请(含推送证书)
一.Mac机上生成请求文件. Mac机上点击证书助手 => 从证书颁发机构请求证书 => 得到CertificateSigningRequest.certSigningRequest请求文 ...