首页 技术 正文
技术 2022年11月13日
0 收藏 319 点赞 2,444 浏览 4048 个字

一: 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: 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];
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905