生活随笔
收集整理的這篇文章主要介紹了
iOS SAX解析XML
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先從網(wǎng)絡(luò)獲取XML文件 NSURL *url = [NSURL URLWithString:@"https://127.0.0.1/videos.xml"];NSURLRequest *request = [NSURLRequest requestWithURL:url];[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {if (connectionError) {NSLog(@"連接錯(cuò)誤 %@", connectionError);return;}NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {// 解析數(shù)據(jù)} else {NSLog(@"服務(wù)器內(nèi)部錯(cuò)誤");}}];
在注釋為解析數(shù)據(jù)處開始解析網(wǎng)絡(luò)獲取的數(shù)據(jù) // 給ViewController添加代理@interface ViewController () <NSXMLParserDelegate>@end// 解析數(shù)據(jù)NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];// 設(shè)置代理parser.delegate = self;// 開始執(zhí)行代理方法[parser parse];
實(shí)現(xiàn)代理方法// 1. 開始解析文檔
- (void)parserDidStartDocument:(NSXMLParser *)parser {NSLog(@"1開始解析");
}// 2. 找開始節(jié)點(diǎn)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {NSLog(@"2找開始節(jié)點(diǎn) %@--%@", elementName, attributeDict);
}// 3. 找節(jié)點(diǎn)之間的內(nèi)容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {NSLog(@"3找節(jié)點(diǎn)之間的內(nèi)容 %@", string);
}// 4. 找結(jié)束節(jié)點(diǎn)
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {NSLog(@"4找結(jié)束節(jié)點(diǎn) %@", elementName);
}// 5. 結(jié)束解析文檔
- (void)parserDidEndDocument:(NSXMLParser *)parser {NSLog(@"5結(jié)束解析");
}// 6. 解析出錯(cuò)
- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {NSLog(@"解析出錯(cuò)");
}
構(gòu)建實(shí)體類
Video.h#import <Foundation/Foundation.h>@interface Video : NSObject
@property (nonatomic, strong) NSNumber *videoId;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *length;
@property (nonatomic, copy) NSString *videoURL;
@property (nonatomic, copy) NSString *imageURL;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *teacher;@property (nonatomic, readonly) NSString *time;- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)videoWithDict:(NSDictionary *)dict;@end
video.m
#import "Video.h"@implementation Video- (instancetype)initWithDict:(NSDictionary *)dict {if (self = [super init]) {[self setValuesForKeysWithDictionary:dict];}return self;
}+ (instancetype)videoWithDict:(NSDictionary *)dict {return [[self alloc] initWithDict:dict];
}- (NSString *)time {int len = self.length.intValue;return [NSString stringWithFormat:@"%02d:%02d:%02d:", len / 3600, (len %3600) / 60, (len % 60) ];
}- (NSString *)description {return [NSString stringWithFormat:@"<%@ : %p> { videoId : %@, name : %@, length : %@, videoURL : %@, imageURL : %@, desc : %@, teacher : %@}", [self class], self, self.videoId, self.name, self.length, self.videoURL, self.imageURL, self.desc, self.teacher];
}@end
XML解析成對(duì)象@interface ViewController () <NSXMLParserDelegate>
// 保存XML數(shù)據(jù)
@property (nonatomic, strong) NSMutableArray *videos;
// 當(dāng)前創(chuàng)建的video對(duì)象
@property (nonatomic, strong) Video *video;
// 保存當(dāng)前節(jié)點(diǎn)內(nèi)容
@property (nonatomic, copy) NSMutableString *string;
@end@implementation ViewController// 懶加載
- (NSMutableArray *)videos {if (_videos == nil) {_videos = [NSMutableArray arrayWithCapacity:10];}return _videos;
}- (NSMutableString *)string {if (_string == nil) {_string = [NSMutableString string];}return _string;
}
@end
在video節(jié)點(diǎn)時(shí)創(chuàng)建Video對(duì)象// 2. 找開始節(jié)點(diǎn)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {NSLog(@"2找開始節(jié)點(diǎn) %@--%@", elementName, attributeDict);// 如果是video標(biāo)簽,創(chuàng)建video對(duì)象if ([elementName isEqualToString:@"video"]) {self.video = [[Video alloc] init];self.video.videoId = @([attributeDict[@"videoId"] intValue]);// 添加到數(shù)組中[self.videos addObject:self.video];}
}
拼接保存每個(gè)節(jié)點(diǎn)中的內(nèi)容// 3. 找節(jié)點(diǎn)之間的內(nèi)容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {NSLog(@"3找節(jié)點(diǎn)之間的內(nèi)容 %@", string);[self.string appendString:string];
}
將內(nèi)容與對(duì)應(yīng)的節(jié)點(diǎn)賦值,并將string對(duì)象置空// 4. 找結(jié)束節(jié)點(diǎn)
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {NSLog(@"4找結(jié)束節(jié)點(diǎn) %@", elementName);// 判斷標(biāo)簽是否有對(duì)應(yīng)的屬性if (![elementName isEqualToString:@"video"] && ![elementName isEqualToString:@"videos"]) {// 為節(jié)點(diǎn)賦值[self.video setValue:self.string forKey:elementName];}// 清空字符串[self.string setString:@""];
}
打印查看結(jié)果
結(jié)果.png videos.xml內(nèi)容
<videos>
<video videoId="1">
<name>01.C語言-語法預(yù)覽</name>
<length>320</length>
<videoURL>/itcast/videos/01.C語言-語法預(yù)覽.mp4</videoURL>
<imageURL>/itcast/images/head1.png</imageURL>
<desc>C語言-語法預(yù)覽</desc>
<teacher>李雷</teacher>
</video>
<video videoId="2">
<name>02.C語言-第一個(gè)C程序</name>
<length>2708</length>
<videoURL>/itcast/videos/02.C語言-第一個(gè)C程序.mp4</videoURL>
<imageURL>/itcast/images/head2.png</imageURL>
<desc>C語言-第一個(gè)C程序</desc>
<teacher>李雷</teacher>
</video>
<video videoId="3">
<name>03.C語言-函數(shù)</name>
<length>822</length>
<videoURL>/itcast/videos/03.C語言-函數(shù).mp4</videoURL>
<imageURL>/itcast/images/head3.png</imageURL>
<desc>C語言-函數(shù)</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="4">
<name>04.C語言-基本數(shù)據(jù)類型</name>
<length>1113</length>
<videoURL>/itcast/videos/04.C語言-基本數(shù)據(jù)類型.mp4</videoURL>
<imageURL>/itcast/images/head4.png</imageURL>
<desc>C語言-基本數(shù)據(jù)類型</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="5">
<name>05.C語言-基本運(yùn)算</name>
<length>594</length>
<videoURL>/itcast/videos/05.C語言-基本運(yùn)算.mp4</videoURL>
<imageURL>/itcast/images/head5.png</imageURL>
<desc>C語言-基本運(yùn)算</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="6">
<name>06.C語言-數(shù)組</name>
<length>923</length>
<videoURL>/itcast/videos/06.C語言-數(shù)組.mp4</videoURL>
<imageURL>/itcast/images/head6.png</imageURL>
<desc>C語言-數(shù)組</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="7">
<name>07.C語言-字符串</name>
<length>848</length>
<videoURL>/itcast/videos/07.C語言-字符串.mp4</videoURL>
<imageURL>/itcast/images/head7.png</imageURL>
<desc>C語言-字符串</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="8">
<name>08.C語言-指針</name>
<length>502</length>
<videoURL>/itcast/videos/08.C語言-指針.mp4</videoURL>
<imageURL>/itcast/images/head8.png</imageURL>
<desc>C語言-指針</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="9">
<name>09.C語言-指針用例</name>
<length>558</length>
<videoURL>/itcast/videos/09.C語言-指針用例.mp4</videoURL>
<imageURL>/itcast/images/head1.png</imageURL>
<desc>C語言-指針用例</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="10">
<name>10.C語言-指針與數(shù)組</name>
<length>1682</length>
<videoURL>/itcast/videos/10.C語言-指針與數(shù)組.mp4</videoURL>
<imageURL>/itcast/images/head2.png</imageURL>
<desc>C語言-指針與數(shù)組</desc>
<teacher>韓梅梅</teacher>
</video>
<video videoId="11">
<name>11.C語言-指針與字符串</name>
<length>1092</length>
<videoURL>/itcast/videos/11.C語言-指針與字符串.mp4</videoURL>
<imageURL>/itcast/images/head3.png</imageURL>
<desc>C語言-指針與字符串</desc>
<teacher>韓梅梅</teacher>
</video>
</videos>
總結(jié)
以上是生活随笔為你收集整理的iOS SAX解析XML的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。