iOS9之后,默认网络请求是https,所有我们要设置一下网络安全,具体设置如下

1.第三方类库 XMLDictionary

下载地址:

https://github.com/nicklockwood/XMLDictionary

 

所用到的xml文件

http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im7eandj

效果如下:

代码实现:

根视图:

rootTableViewController.m文件

#import "rootTableViewController.h"
#import "XMLDictionary.h"
#import "secondViewController.h"
@interface rootTableViewController ()
@property(nonatomic,strong)NSArray *cityArr;
@end @implementation rootTableViewController - (void)viewDidLoad {
[super viewDidLoad]; NSString *path=@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im77fqda"; NSURL *url=[NSURL URLWithString:path]; NSData *data=[NSData dataWithContentsOfURL:url]; XMLDictionaryParser *parser=[[XMLDictionaryParser alloc]init];
NSDictionary *dic=[parser dictionaryWithData:data];
self.cityArr=[NSArray arrayWithArray:dic[@"divisions"][@"division"]]; NSLog(@"%@",self.cityArr);
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"]; self.title=@"城市列表"; // Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cityArr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath]; cell.textLabel.text=self.cityArr[indexPath.row][@"name"];
return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
secondViewController *sec=[[secondViewController alloc]init];
sec.location=self.cityArr[indexPath.row][@"location"];
sec.title=self.cityArr[indexPath.row][@"name"];
[self.navigationController pushViewController:sec animated:YES]; } @end

第二个视图:secondViewController.h

#import <UIKit/UIKit.h>

@interface secondViewController : UIViewController
@property(nonatomic,strong)NSDictionary *location;
@property(nonatomic,strong)NSString *title; @end

secondViewController.m文件

#import "secondViewController.h"

@interface secondViewController ()
@property(nonatomic,strong)UILabel *latitudeName;
@property(nonatomic,strong)UILabel *longitudeName;
@property(nonatomic,strong)UILabel *timezoneName;
@property(nonatomic,strong)UILabel *latitude;
@property(nonatomic,strong)UILabel *longitude;
@property(nonatomic,strong)UILabel *timezone;
@end @implementation secondViewController - (void)viewDidLoad {
[super viewDidLoad];
[self setKongjian]; self.title=[NSString stringWithFormat:@"%@的经纬度",self.title]; self.view.backgroundColor=[UIColor colorWithRed:0.148 green:1.000 blue:0.946 alpha:1.000]; }
-(void)setKongjian{
self.latitudeName=[[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
self.latitudeName.text=@"纬度:";
self.latitude=[[UILabel alloc]initWithFrame:CGRectMake(150, 200, 200, 30)];
self.latitude.text=self.location[@"latitude"]; self.longitudeName=[[UILabel alloc]initWithFrame:CGRectMake(100, 250, 100, 30)];
self.longitudeName.text=@"经度:";
self.longitude=[[UILabel alloc]initWithFrame:CGRectMake(150, 250, 200, 30)];
self.longitude.text=self.location[@"longitude"]; self.timezoneName=[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 30)];
self.timezoneName.text=@"时区:";
self.timezone=[[UILabel alloc]initWithFrame:CGRectMake(150, 300, 200, 30)];
self.timezone.text=self.location[@"timezone"]; [self.view addSubview:self.latitudeName];
[self.view addSubview:self.longitudeName];
[self.view addSubview:self.timezoneName]; [self.view addSubview:self.latitude];
[self.view addSubview:self.longitude];
[self.view addSubview:self.timezone]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

2.GDataXMLNode类库

具体配置过程如下

核心代码

#import "rootTableViewController.h"
#import "GDataXMLNode.h"
#import "secondViewController.h"
@interface rootTableViewController ()
@property(nonatomic,strong)NSMutableDictionary *location;
@property(nonatomic,strong)NSMutableArray *locationArr;
@end @implementation rootTableViewController - (void)viewDidLoad {
[super viewDidLoad]; self.locationArr=[NSMutableArray array];
// 获取网络上的xml
NSURL *url=[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im7envub"]; NSData *data=[NSData dataWithContentsOfURL:url]; // 使用NSData对象初始化
GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil]; // 获取根节点
GDataXMLElement *rootElement=[doc rootElement]; // 获取根节点以下的节点
GDataXMLElement *divisions=[[rootElement elementsForName:@"divisions"] objectAtIndex:0];
NSArray *division=[divisions elementsForName:@"division"]; // NSLog(@"%@",division);
for (GDataXMLElement *div in division) {
self.location=[NSMutableDictionary dictionary];
// 获取name的节点
GDataXMLElement *nameElement=[[div elementsForName:@"name"] objectAtIndex:0];
NSString *name=[nameElement stringValue]; // 获取location 的节点
GDataXMLElement *location=[[div elementsForName:@"location"] objectAtIndex:0]; // 获取latitude 的节点
GDataXMLElement *latitudeElement=[[location elementsForName:@"latitude"] objectAtIndex:0];
NSString *latitude=[latitudeElement stringValue]; // 获取longitude 的节点
GDataXMLElement *longitudeElement=[[location elementsForName:@"longitude"] objectAtIndex:0];
NSString *longitude=[longitudeElement stringValue]; // 把他们的值加到一个=字典中
[self.location setObject:name forKey:@"name"];
[self.location setObject:latitude forKey:@"latitude"];
[self.location setObject:longitude forKey:@"longitude"]; // 把字典添加到可变集合中
[self.locationArr addObject:self.location]; }
self.title=@"城市列表";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.locationArr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath]; cell.textLabel.text=self.locationArr[indexPath.row][@"name"];
return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
secondViewController *sec=[[secondViewController alloc]init];
// 把字典传递到第二个页面
sec.location=self.locationArr[indexPath.row];
[self.navigationController pushViewController:sec animated:YES]; } @end

第二个页面类似

3.系统自带的

核心代码

#import "rootTableViewController.h"
#import "secondViewController.h"
@interface rootTableViewController ()<NSXMLParserDelegate>
@property(nonatomic,strong)NSMutableArray *arr;
@property(nonatomic,strong)NSMutableDictionary *dic; @property(nonatomic,strong)NSString *str;
@end @implementation rootTableViewController - (void)viewDidLoad {
[super viewDidLoad]; NSURL *url=[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im7mg21x"];
NSData *data=[NSData dataWithContentsOfURL:url]; NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data]; parser.delegate=self; BOOL bol=[parser parse];
NSLog(@"%d",bol);
self.title=@"城市列表";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"]; } -(void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"start");
self.arr=[NSMutableArray array];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"end");
NSLog(@"%@",self.arr);
} -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{ if ([elementName isEqualToString:@"division"]) {
self.dic=[NSMutableDictionary dictionary]; [self.dic setDictionary:attributeDict];
}
} -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"name" ]||[elementName isEqualToString:@"latitude"]||[elementName isEqualToString:@"longitude"]) {
[self.dic setObject:self.str forKey:elementName];
}else if ([elementName isEqualToString:@"division"]){
[self.arr addObject:self.dic];
} }
-(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)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath]; cell.textLabel.text=self.arr[indexPath.row][@"name"];
return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
secondViewController *sec=[[secondViewController alloc]init];
sec.location=self.arr[indexPath.row];
[self.navigationController pushViewController:sec animated:YES]; }
@end

iOS--XML三种解析方法( XMLDictionary)、(GDataXMLNode)、(NSXMLParser)的更多相关文章

  1. iOS中 三种随机数方法详解

    ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...

  2. iOS xml文件的解析方式 XMLDictionary,GDataXMLNode,NSXMLParser

    iOS9之后,默认网络请求是https,所有我们要设置一下网络安全,具体设置如下 1.第三方类库 XMLDictionary 下载地址: https://github.com/nicklockwood ...

  3. QT XML文档的解析 QXmlStreamReader, DOM,SAX 三种解析方法 简单示例

    0. xml文档如下 <?xml version="1.0"?> <bookindex> <entry term="sidebearings ...

  4. Python_XML的三种解析方法

    什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这 ...

  5. <爬虫实战>豆瓣电影TOP250(三种解析方法)

    1.豆瓣电影排行.py # 目标:爬取豆瓣电影排行榜TOP250的电影信息 # 信息包括:电影名字,上映时间,主演,评分,导演,一句话评价 # 解析用学过的几种方法都实验一下①正则表达式.②Beaut ...

  6. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

  7. iOS——浅谈iOS中三种生成随机数方法

    ios 有如下三种随机数方法:

  8. Qt中三种解析xml的方式

    在下面的随笔中,我会根据xml的结构,给出Qt中解析这个xml的三种方式的代码.虽然,这个代码时通过调用Qt的函数实现的,但是,很多开源的C++解析xml的库,甚至很多其他语言解析xml的库,都和下面 ...

  9. iOS-浅谈iOS中三种生成随机数方法

    ios 有如下三种随机数方法:

随机推荐

  1. 【原创】验证代理IP是否有用

    /// <summary> /// 验证代理IP是否有用 /// </summary> /// <param name="ip">IP地址< ...

  2. 介绍开源的.net通信框架NetworkComms框架 源码分析(十)DOSProtection

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  3. Saas

    SaaS是Software-as-a-Service(软件即服务)的简称,随着互联网技术的发展和应用软件的成熟, 在21世纪开始兴起的一种完全创新的软件应用模式.它与“on-demand softwa ...

  4. JavaMail API 详细分解

    在使用Spring框架的过程中,它的优势之一就是在于跟其他一些技术的整合,如JavaMail .任务调度.缓存策略等技术.今天就Java Mail详细阐述.JavaMail API是被设计为与协议无关 ...

  5. php自动载入类的实践

    <?php //function __autoload($class_name) {//    require_once $class_name . '.php';//} spl_autoloa ...

  6. Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值

    jquery取radio单选按钮的值 $("input[name='items']:checked").val();  另:判断radio是否选中并取得选中的值 如下所示: fun ...

  7. Java经典实例:在正则表达式中控制大小写

    默认是:区分大小写的: 传递标志参数:Pattern.CASE_INSENSITIVE,以说明匹配时忽略大小写:如果你的代码运行在不同的地区那么你应该再添加一个Pattern.UNICODE_CASE ...

  8. JVM的生命周期、体系结构、内存管理和垃圾回收机制

    一.JVM的生命周期 JVM实例:一个独立运行的java程序,是进程级别 JVM执行引擎:用户运行程序的线程,是JVM实例的一部分 JVM实例的诞生 当启动一个java程序时.一个JVM实例就诞生了, ...

  9. 15个优秀的 Material Design(材料设计)案例

    Material Design (材料设计)是由谷歌创建和设计的一种设计语言,结合成功的设计的经典原则以及创新科技.谷歌的目标是开发一个设计系统,让所有的产品在任何平台上拥有统一的用户体验.全新的设计 ...

  10. Space.js – HTML 驱动的页面 3D 滚动效果

    为了让我们的信息能够有效地沟通,我们需要创建用户和我们的媒体之间的强有力的联系.今天我们就来探讨在网络上呈现故事的新方法,并为此创造了一个开源和免费使用的 JavaScript 库称为 space.j ...