-
2.0 XML解析
2.1 XML简单介绍
(1) XML:可扩展标记语言
a.语法
b.XML文档的三部分(声明、元素和属性)
c.其它注意点(注意不能交叉包含、空行换行、XML文档只能有一个根元素等)
(2) XML解析
a.XML解析的两种方式
001 SAX:从根元素开始,按顺序一个元素一个元素的往下解析,可用于解析大、小文件
002 DOM:一次性将整个XML文档加载到内存中,适合较小的文件
b.解析XML的工具
001 苹果原生NSXMLParser:使用SAX方式解析,使用简单
002 第三方框架
libxml2:纯C语言的,默认包含在iOS SDK中,同时支持DOM和SAX的方式解析
GDataXML:采用DOM方式解析,该框架由Goole开发,是基于xml2的
- 2.2 XML解析
(1)使用NSXMLParser解析XML步骤和代理方法
//解析步骤:
//4.1 创建一个解析器
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
//4.2 设置代理
parser.delegate = self;
//4.3 开始解析
[parser parse];
-----------------------------------------
//1.开始解析XML文档
-(void)parserDidStartDocument:(nonnull NSXMLParser *)parser
//2.开始解析XML中某个元素的时候调用,比如<video>
-(void)parser:(nonnull NSXMLParser *)parser didStartElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(nonnull NSDictionary<NSString *,NSString *> *)attributeDict
{
if ([elementName isEqualToString:@"videos"]) {
return;
}
//字典转模型
XMGVideo *video = [XMGVideo objectWithKeyValues:attributeDict];
[self.videos addObject:video];
}
//3.当某个元素解析完成之后调用,比如</video>
-(void)parser:(nonnull NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
//4.XML文档解析结束
-(void)parserDidEndDocument:(nonnull NSXMLParser *)parser
XML解析demo
#import "ViewController.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#import "VideosItem.h"
#import <MediaPlayer/MediaPlayer.h>
#define baseUrl @"http://120.25.226.186:32812"
@interface ViewController ()<NSXMLParserDelegate>
@property(nonatomic,strong)NSMutableArray * videos ;
@end
@implementation ViewController
-(NSMutableArray *)videos
{
if (_videos==nil) {
_videos = [NSMutableArray new];
}
return _videos;
}
- (void)viewDidLoad {
[super viewDidLoad];
[VideosItem mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"ID":@"id"};
}];
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) {
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
parser.delegate = self;
[parser parse];
[self.tableView reloadData];
}];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.videos.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"videos";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
VideosItem *item = self.videos[indexPath.row];
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.length;
NSString *imageUrl = [baseUrl stringByAppendingPathComponent:item.image];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"Snip20160225_341"]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VideosItem *item = self.videos[indexPath.row];
NSString *url = [baseUrl stringByAppendingPathComponent:item.url];
MPMoviePlayerViewController * vc = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:vc animated:YES completion:nil];
}
-(void)parser:(nonnull NSXMLParser *)parser didStartElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(nonnull NSDictionary<NSString *,NSString *> *)attributeDict
{
if ([elementName isEqualToString:@"videos"]) {
return;
}
VideosItem *item = [VideosItem mj_objectWithKeyValues:attributeDict];
[self.videos addObject:item];
}
@end