首页 > 移动平台 > 详细

2016 - 1- 23 iOS中xml解析 (!!!!!!!有坑要解决!!!!!!)

时间:2016-01-23 22:56:52      阅读:182      评论:0      收藏:0      [点我收藏+]

一: iOS中xml解析的几种方式简介

  1.官方原生

    NSXMLParser :SAX方式解析,使用起来比较简单

  2.第三方框架

    libxml2 :纯C 同时支持DOM与SAX

    GDataXML: DOM方式解析,谷歌开发,基于libxml2的。是OC中的框架。

 3.如果是小文件 建议使用 GDataXML;如果是大文件建议使用NSXMLParser

 

 

二: NSXMLParser与GDateXML

   1.NSXMLParser的用法

    QAQ 试了好几次失败了,暂时没用分析出原因,先把代码放在这里,以后再回来看看吧!

    问题: parser的代理方法死活不调用 真的是分析不出来是为什么。。。。打印了XML数据也没问题。

//
//  TableViewController.m
//  json
//
//  Created by Mac on 16/1/23.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "TableViewController.h"
#import <UIImageView+WebCache.h>
#import <MediaPlayer/MediaPlayer.h>
#import "ZZVideo.h"
#import <MJExtension.h>
@interface TableViewController ()<NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray  *videos;


@end

@implementation TableViewController
- (NSMutableArray *)videos{
    if (!_videos) {
        _videos = [NSMutableArray array];
//        NSLog(@"%s",__func__);
    }
    return _videos;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
    
    NSURLRequest *request =  [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
       // 解析xml数据
         // 创建XML解析器
       NSXMLParser *parser =  [[NSXMLParser alloc] initWithData:data];
        
//        NSLog(@"%@",self.videos);
         // 开始解析
        [parser parse];
        
        // 设置代理
        parser.delegate = self;
        NSLog(@"%@",parser.delegate);
        
//        NSLog(@"%zd",_videos.count);
        [self.tableView reloadData];
    }];
    
}

#pragma mark - Table view data source


// 开始解析文件
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"%s",__func__);
    
}
// 结束解析某文件
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    
}
//结束解析某元素
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    
}
// 开始解析某元素
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    NSLog(@"%s",__func__);
    if ([elementName isEqualToString:@"videos"]) {
        return ;
    }
    ZZVideo *video = [ZZVideo mj_objectWithKeyValues:attributeDict];
    [self.videos addObject:video];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return self.videos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *ID = @"video";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        ZZVideo *video = self.videos[indexPath.row];
        
        cell.detailTextLabel.text = [NSString stringWithFormat:@"视频时长: %ld",(long)video.length];
        
        cell.textLabel.text= video.name;
        
        NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
        
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:nil];
        
        
        return cell;
}
#pragma mark - 代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        ZZVideo *video = self.videos[indexPath.row];
        
        NSString *videoStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
        
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoStr]];
       
        [self presentViewController:vc animated:YES completion:nil];
 
}
@end

 

  2.GDateXML

  这个是谷歌开发的第三方框架。加载方式是DOM

  这是与上面不同的地方的代码:

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
      // 创建文档加载数据
        GDataXMLDocument *doc =    [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
      
        // 获得根节点以后的所有元素
        NSArray *elements = [doc.rootElement elementsForName:@"video"];
        
        for (GDataXMLElement *element in elements) {
            ZZVideo *video = [[ZZVideo alloc] init];
            video.name = [element attributeForName:@"name"].stringValue;
            video.length = [element attributeForName:@"length"].stringValue.integerValue;
            video.image = [element attributeForName:@"image"].stringValue;
            video.url = [element attributeForName:@"url"].stringValue;
            [self.videos addObject:video];
        }

 

2016 - 1- 23 iOS中xml解析 (!!!!!!!有坑要解决!!!!!!)

原文:http://www.cnblogs.com/BJTUzhengli/p/5154063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!