ios xmlios xml转dictionaryy 怎么插入列表元素

今天看啥 热点:
iOS xml文件的解析方式 XMLDictionary,GDataXMLNode,NSXMLParser,nsxmlparseriOS9之后,默认网络请求是https,所有我们要设置一下网络安全,具体设置如下
1.第三方类库&XMLDictionary
下载地址:
/nicklockwood/XMLDictionary
所用到的xml文件
/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 *cityA
@implementation rootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path=@"/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.editButtonI
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cityArr.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
cell.textLabel.text=self.cityArr[indexPath.row][@"name"];
-(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];
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
#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.
第二个视图:secondViewController.h
#import &UIKit/UIKit.h&
@interface secondViewController : UIViewController
@property(nonatomic,strong)NSDictionary *
@property(nonatomic,strong)NSString *
secondViewController.m文件
#import "secondViewController.h"
@interface secondViewController ()
@property(nonatomic,strong)UILabel *latitudeN
@property(nonatomic,strong)UILabel *longitudeN
@property(nonatomic,strong)UILabel *timezoneN
@property(nonatomic,strong)UILabel *
@property(nonatomic,strong)UILabel *
@property(nonatomic,strong)UILabel *
@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.
#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.
2.GDataXMLNode类库
具体配置过程如下
#import "rootTableViewController.h"
#import "GDataXMLNode.h"
#import "secondViewController.h"
@interface rootTableViewController ()
@property(nonatomic,strong)NSMutableDictionary *
@property(nonatomic,strong)NSMutableArray *locationA
@implementation rootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationArr=[NSMutableArray array];
获取网络上的xml
NSURL *url=[NSURL URLWithString:@"/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.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
cell.textLabel.text=self.locationArr[indexPath.row][@"name"];
-(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];
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
#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.
第二个页面类似
3.系统自带的
#import "rootTableViewController.h"
#import "secondViewController.h"
@interface rootTableViewController ()&NSXMLParserDelegate&
@property(nonatomic,strong)NSMutableArray *
@property(nonatomic,strong)NSMutableDictionary *
@property(nonatomic,strong)NSString *
@implementation rootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url=[NSURL URLWithString:@"/api/v1/divisions?mtt=1.help%2Fapi.0.0.im7mg21x"];
NSData *data=[NSData dataWithContentsOfURL:url];
NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data];
parser.delegate=
BOOL bol=[parser parse];
NSLog(@"%d",bol);
self.title=@"城市列表";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
// 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.editButtonI
-(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.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
cell.textLabel.text=self.arr[indexPath.row][@"name"];
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
secondViewController *sec=[[secondViewController alloc]init];
sec.location=self.arr[indexPath.row];
[self.navigationController pushViewController:sec animated:YES];
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
#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.
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新鍗氬?鍒嗙被锛}

我要回帖

更多关于 xmldictionary 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信