-
2.0 XML解析
2.1 XML簡(jiǎn)單介紹
(1) XML:可擴(kuò)展標(biāo)記語(yǔ)言
a.語(yǔ)法
b.XML文檔的三部分(聲明狐蜕、元素和屬性)
c.其它注意點(diǎn)(注意不能交叉包含摩疑、空行換行育谬、XML文檔只能有一個(gè)根元素等)
(2) XML解析
a.XML解析的兩種方式
001 SAX:從根元素開(kāi)始啦撮,按順序一個(gè)元素一個(gè)元素的往下解析,可用于解析大、小文件
002 DOM:一次性將整個(gè)XML文檔加載到內(nèi)存中滋饲,適合較小的文件
b.解析XML的工具
001 蘋(píng)果原生NSXMLParser:使用SAX方式解析,使用簡(jiǎn)單
002 第三方框架
libxml2:純C語(yǔ)言的喊巍,默認(rèn)包含在iOS SDK中屠缭,同時(shí)支持DOM和SAX的方式解析
GDataXML:采用DOM方式解析,該框架由Goole開(kāi)發(fā)崭参,是基于xml2的
- 2.2 XML解析
(1)使用NSXMLParser解析XML步驟和代理方法
//解析步驟:
//4.1 創(chuàng)建一個(gè)解析器
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
//4.2 設(shè)置代理
parser.delegate = self;
//4.3 開(kāi)始解析
[parser parse];
-----------------------------------------
//1.開(kāi)始解析XML文檔
-(void)parserDidStartDocument:(nonnull NSXMLParser *)parser
//2.開(kāi)始解析XML中某個(gè)元素的時(shí)候調(diào)用呵曹,比如<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;
}
//字典轉(zhuǎn)模型
XMGVideo *video = [XMGVideo objectWithKeyValues:attributeDict];
[self.videos addObject:video];
}
//3.當(dāng)某個(gè)元素解析完成之后調(diào)用,比如</video>
-(void)parser:(nonnull NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
//4.XML文檔解析結(jié)束
-(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