zoukankan      html  css  js  c++  java
  • 源码0602-03-掌握-XML解析-NSXMLParser

    //
    //  ViewController.m
    //  01-掌握-JSON解析
    #import "ViewController.h"
    #import <UIImageView+WebCache.h>
    #import <MediaPlayer/MediaPlayer.h>
    #import "XMGVideo.h"
    #import <MJExtension.h>
    
    @interface ViewController () <NSXMLParserDelegate>
    /** 视频数据 */
    @property (nonatomic, strong) NSMutableArray *videos;
    @end
    
    @implementation ViewController
    
    - (NSMutableArray *)videos
    {
        if (!_videos) {
            _videos = [NSMutableArray array];
        }
        return _videos;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 0.请求路径
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
        
        // 1.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // 2.发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 创建XML解析器
            NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    
            // 设置代理
            parser.delegate = self;
    
            // 开始解析XML
            [parser parse];
    
            // 刷新表格
            [self.tableView reloadData];
        }];
    }
    
    #pragma mark - <NSXMLParserDelegate>
    /**
     * 解析到某个元素的结尾(比如解析</videos>)
     */
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
    //    NSLog(@"didEndElement - %@", elementName);
    }
    
    /**
     * 解析到某个元素的开头(比如解析<videos>)
     */
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementName isEqualToString:@"videos"]) return;
    //    XMGVideo *video = [[XMGVideo alloc] init];
    //    video.keyValues = attributeDict;
        
        XMGVideo *video = [XMGVideo objectWithKeyValues:attributeDict];
        [self.videos addObject:video];
    }
    
    /**
     * 开始解析XML文档
     */
    - (void)parserDidStartDocument:(NSXMLParser *)parser
    {
    //    NSLog(@"parserDidStartDocument");
    }
    
    /**
     * 解析完毕
     */
    - (void)parserDidEndDocument:(NSXMLParser *)parser
    {
    //    NSLog(@"parserDidEndDocument");
    }
    
    
    #pragma mark - 数据源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.videos.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"video";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        XMGVideo *video = self.videos[indexPath.row];
        
        cell.textLabel.text = video.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%zd", video.length];
        
        NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        return cell;
    }
    
    #pragma mark - 代理方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        XMGVideo *video = self.videos[indexPath.row];
        NSString *urlStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
        
        // 创建视频播放器
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
    
        // 显示视频
        [self presentViewController:vc animated:YES completion:nil];
    }
    
    @end
    //
    //  XMGVideo.h
    //  01-掌握-JSON解析
    #import <Foundation/Foundation.h>
    
    @interface XMGVideo : NSObject
    /** ID */
    @property (nonatomic, assign) NSInteger ID;
    /** 视频名字 */
    @property (nonatomic, copy) NSString *name;
    /** 视频时长 */
    @property (nonatomic, assign) NSInteger length;
    /** 视频图片 */
    @property (nonatomic, copy) NSString *image;
    /** 视频文件路径 */
    @property (nonatomic, copy) NSString *url;
    @end
    //
    //  XMGVideo.m
    //  01-掌握-JSON解析
    #import "XMGVideo.h"
    
    @implementation XMGVideo
    //+ (NSDictionary *)replacedKeyFromPropertyName
    //{
    //    return @{@"ID" : @"id",
    //             @"desc" : @"description"};
    //}
    @end

    04-掌握-XML解析-GDataXML

    //
    //  ViewController.m
    //  01-掌握-JSON解析
    
    #import "ViewController.h"
    #import <UIImageView+WebCache.h>
    #import <MediaPlayer/MediaPlayer.h>
    #import "XMGVideo.h"
    #import <MJExtension.h>
    #import "GDataXMLNode.h"
    
    @interface ViewController ()
    /** 视频数据 */
    @property (nonatomic, strong) NSMutableArray *videos;
    @end
    
    @implementation ViewController
    
    - (NSMutableArray *)videos
    {
        if (!_videos) {
            _videos = [NSMutableArray array];
        }
        return _videos;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 0.请求路径
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
        
        // 1.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // 2.发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 加载整个文档
            GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
            
            // 获得所有video元素
            NSArray *elements = [doc.rootElement elementsForName:@"video"];
            for (GDataXMLElement *ele in elements) {
                XMGVideo *video = [[XMGVideo alloc] init];
                video.name = [ele attributeForName:@"name"].stringValue;
                video.url = [ele attributeForName:@"url"].stringValue;
                video.image = [ele attributeForName:@"image"].stringValue;
                video.length = [ele attributeForName:@"length"].stringValue.integerValue;
                
                [self.videos addObject:video];
            }
            
            // 刷新表格
            [self.tableView reloadData];
        }];
    }
    
    #pragma mark - 数据源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.videos.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"video";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        XMGVideo *video = self.videos[indexPath.row];
        
        cell.textLabel.text = video.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%zd", video.length];
        
        NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        return cell;
    }
    
    #pragma mark - 代理方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        XMGVideo *video = self.videos[indexPath.row];
        NSString *urlStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
        
        // 创建视频播放器
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
    
        // 显示视频
        [self presentViewController:vc animated:YES completion:nil];
    }
    
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    ACL的基本访问列表与高级访问列表
    ACL配置
    OSPF与ACL综合应用
    RSTP基础配置
    基于接口地址池和基于全局配置的DHCP
    在ensp上通过FTP进行文件操作
    在ensp上配置通过Stelnet登录系统
    在ensp上配置通过Telent登录系统
    在eNSP上简单的模拟企业网络场景(不同网段互连)
    虚拟机中使用Samba实现文件共享
  • 原文地址:https://www.cnblogs.com/laugh/p/6595800.html
Copyright © 2011-2022 走看看