1.官方原生
NSXMLParser :SAX方式解析,使用起来比较简单
2.第三方框架
libxml2 :纯C 同时支持DOM与SAX
GDataXML: DOM方式解析,谷歌开发,基于libxml2的。是OC中的框架。
3.如果是小文件 建议使用 GDataXML;如果是大文件建议使用NSXMLParser
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