IOS XML解析解析方法SAX
来源:互联网 发布:2017淘宝c店 编辑:程序博客网 时间:2024/06/05 10:43
XML 格式 (一) 带属性的XML
<?xml version="1.0" encoding="UTF-8"?><vedios><vedio ID = "01" name = "小黄人 第01部" length = "12" image = "http://127.0.0.1/resources/images/minion_01.png" url = "http://127.0.0.1/resources/videos/minion_01.mp4" /> <vedio ID = "02" name = "小黄人 第02部" length = "14" image = "http://127.0.0.1/resources/images/minion_02.png" url = "http://127.0.0.1/resources/videos/minion_02.mp4" /> <vedio ID = "03" name = "小黄人 第03部" length = "16" image = "http://127.0.0.1/resources/images/minion_03.png" url = "http://127.0.0.1/resources/videos/minion_03.mp4" /> <vedio ID = "04" name = "小黄人 第04部" length = "18" image = "http://127.0.0.1/resources/images/minion_04.png" url = "http://127.0.0.1/resources/videos/minion_04.mp4" /> <vedio ID = "05" name = "小黄人 第05部" length = "10" image = "http://127.0.0.1/resources/images/minion_05.png" url = "http://127.0.0.1/resources/videos/minion_05.mp4" /> <vedio ID = "06" name = "小黄人 第06部" length = "12" image = "http://127.0.0.1/resources/images/minion_06.png" url = "http://127.0.0.1/resources/videos/minion_06.mp4" /> <vedio ID = "07" name = "小黄人 第07部" length = "16" image = "http://127.0.0.1/resources/images/minion_07.png" url = "http://127.0.0.1/resources/videos/minion_07.mp4" /> <vedio ID = "08" name = "小黄人 第08部" length = "18" image = "http://127.0.0.1/resources/images/minion_08.png" url = "http://127.0.0.1/resources/videos/minion_08.mp4" /> <vedio ID = "09" name = "小黄人 第09部" length = "20" image = "http://127.0.0.1/resources/images/minion_09.png" url = "http://127.0.0.1/resources/videos/minion_09.mp4" /> <vedio ID = "10" name = "小黄人 第10部" length = "12" image = "http://127.0.0.1/resources/images/minion_10.png" url = "http://127.0.0.1/resources/videos/minion_10.mp4" /> <vedio ID = "11" name = "小黄人 第11部" length = "13" image = "http://127.0.0.1/resources/images/minion_11.png" url = "http://127.0.0.1/resources/videos/minion_11.mp4" /> <vedio ID = "12" name = "小黄人 第12部" length = "12" image = "http://127.0.0.1/resources/images/minion_12.png" url = "http://127.0.0.1/resources/videos/minion_12.mp4" /> <vedio ID = "13" name = "小黄人 第13部" length = "11" image = "http://127.0.0.1/resources/images/minion_13.png" url = "http://127.0.0.1/resources/videos/minion_13.mp4" /> <vedio ID = "14" name = "小黄人 第14部" length = "15" image = "http://127.0.0.1/resources/images/minion_14.png" url = "http://127.0.0.1/resources/videos/minion_14.mp4" /> <vedio ID = "15" name = "小黄人 第15部" length = "14" image = "http://127.0.0.1/resources/images/minion_15.png" url = "http://127.0.0.1/resources/videos/minion_15.mp4" /> <vedio ID = "16" name = "小黄人 第16部" length = "18" image = "http://127.0.0.1/resources/images/minion_16.png" url = "http://127.0.0.1/resources/videos/minion_16.mp4" /></vedios>
解析方式
// 加载网络数据.// urlString :网络接口!-(void)loadServerDataWithUrlString:(NSString *)urlString{ // 1.创建请求 NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 2.发送请求 [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 一般在网络开发中,必须处理错误机制! if (data && !error) { // 调试 NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); // XML文档特点: 需要的数据都在 属性中.需要 属性名称和属性内容! // XML解析: SAX解析! // 1. 实例化 XML 的 SAX 解析器! NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; // 2. 设置解析器代理. parser.delegate = self; // 3. 开始解析 XML 文档 // 一旦调用了下面的开始解析方法,就会自动调用代理方法,解析 XML 文档! [parser parse]; // 注意: 数据解析结束之后,记得刷新数据源! dispatch_async(dispatch_get_main_queue(), ^{ [self reloadData]; }); }else { NSLog(@"网络连接错误..."); } }] resume]; }#pragma NSXMLParserDelegate- (void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"1.XML文档解析开始!");}// 开始解析元素的时候就会调用!XML文档中有多少个元素就会调用多个次!// elementName:元素名称!// attributeDict:属性字典!当前元素对应的属性字典!- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{ NSLog(@"2.开始解析:%@元素,元素属性是:%@",elementName,attributeDict); // 判断:只有元素 vedio 的属性字典是需要的内容! if ([elementName isEqualToString:@"vedio"]) { // 字典转模型 CZVideo *video = [CZVideo videoWithDict:attributeDict]; // 添加到数据源中 [self.videos addObject:video]; }}// 发现元素内容的时候就会调用!// string :元素内容!- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ NSLog(@"3.发现元素内容:%@",string);}// 元素解析结束的时候就会调用,XML文档中有多少个元素就会调用多个次!// elementName :元素名称- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{ NSLog(@"4.元素%@解析结束",elementName);}- (void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"5.XML文档解析结束!");}
第二种XML 格式 嵌套XML ,不带属性的XML
<?xml version="1.0" encoding="UTF-8"?><vedios><vedio><ID>01</ID><image>http://127.0.0.1/resources/images/minion_01.png</image><length>12</length><name>小黄人 第01部</name><url>http://127.0.0.1/resources/videos/minion_01.mp4</url></vedio> <vedio> <ID>02</ID> <image>http://127.0.0.1/resources/images/minion_02.png</image> <length>14</length> <name>小黄人 第02部</name> <url>http://127.0.0.1/resources/videos/minion_02.mp4</url> </vedio> <vedio> <ID>03</ID> <image>http://127.0.0.1/resources/images/minion_03.png</image> <length>16</length> <name>小黄人 第03部</name> <url>http://127.0.0.1/resources/videos/minion_03.mp4</url> </vedio> <vedio> <ID>04</ID> <image>http://127.0.0.1/resources/images/minion_04.png</image> <length>16</length> <name>小黄人 第04部</name> <url>http://127.0.0.1/resources/videos/minion_04.mp4</url> </vedio> <vedio> <ID>05</ID> <image>http://127.0.0.1/resources/images/minion_05.png</image> <length>18</length> <name>小黄人 第05部</name> <url>http://127.0.0.1/resources/videos/minion_05.mp4</url> </vedio> <vedio> <ID>06</ID> <image>http://127.0.0.1/resources/images/minion_06.png</image> <length>20</length> <name>小黄人 第06部</name> <url>http://127.0.0.1/resources/videos/minion_06.mp4</url> </vedio> <vedio> <ID>07</ID> <image>http://127.0.0.1/resources/images/minion_07.png</image> <length>22</length> <name>小黄人 第07部</name> <url>http://127.0.0.1/resources/videos/minion_07.mp4</url> </vedio> <vedio> <ID>08</ID> <image>http://127.0.0.1/resources/images/minion_08.png</image> <length>24</length> <name>小黄人 第08部</name> <url>http://127.0.0.1/resources/videos/minion_08.mp4</url> </vedio> <vedio> <ID>09</ID> <image>http://127.0.0.1/resources/images/minion_09.png</image> <length>26</length> <name>小黄人 第09部</name> <url>http://127.0.0.1/resources/videos/minion_09.mp4</url> </vedio> <vedio> <ID>10</ID> <image>http://127.0.0.1/resources/images/minion_10.png</image> <length>28</length> <name>小黄人 第10部</name> <url>http://127.0.0.1/resources/videos/minion_10.mp4</url> </vedio> <vedio> <ID>11</ID> <image>http://127.0.0.1/resources/images/minion_11.png</image> <length>30</length> <name>小黄人 第11部</name> <url>http://127.0.0.1/resources/videos/minion_11.mp4</url> </vedio> <vedio> <ID>12</ID> <image>http://127.0.0.1/resources/images/minion_12.png</image> <length>32</length> <name>小黄人 第12部</name> <url>http://127.0.0.1/resources/videos/minion_12.mp4</url> </vedio> <vedio> <ID>13</ID> <image>http://127.0.0.1/resources/images/minion_13.png</image> <length>34</length> <name>小黄人 第13部</name> <url>http://127.0.0.1/resources/videos/minion_13.mp4</url> </vedio> <vedio> <ID>14</ID> <image>http://127.0.0.1/resources/images/minion_14.png</image> <length>36</length> <name>小黄人 第14部</name> <url>http://127.0.0.1/resources/videos/minion_14.mp4</url> </vedio> <vedio> <ID>15</ID> <image>http://127.0.0.1/resources/images/minion_15.png</image> <length>38</length> <name>小黄人 第15部</name> <url>http://127.0.0.1/resources/videos/minion_15.mp4</url> </vedio> <vedio> <ID>16</ID> <image>http://127.0.0.1/resources/images/minion_16.png</image> <length>40</length> <name>小黄人 第16部</name> <url>http://127.0.0.1/resources/videos/minion_16.mp4</url> </vedio></vedios>
#pragma NSXMLParserDelegate- (void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"1.XML文档解析开始!");}// 开始解析元素的时候就会调用!XML文档中有多少个元素就会调用多个次!// elementName:元素名称!// attributeDict:属性字典!当前元素对应的属性字典!- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{ NSLog(@"2.开始解析:%@元素,元素属性是:%@",elementName,attributeDict); if ([elementName isEqualToString:@"vedio"]) { //根据元素名称,创建数据模型! self.video = [[CZVideo alloc] init]; } }// 发现元素内容的时候就会调用!// string :元素内容!- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ NSLog(@"3.发现元素内容:%@",string); // 使用定义好的字符串容器接收当前元素内容! // setString :使用后面的值,替换掉之前的字符串内容! [self.currentElementString setString:string];}// 元素解析结束的时候就会调用,XML文档中有多少个元素就会调用多个次!// elementName :元素名称- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{ NSLog(@"4.元素%@解析结束",elementName); // 将对应之后的值直接存储在数据模型中! // if ([elementName isEqualToString:@"ID"]) {// [self.video setValue:self.currentElementString forKey:elementName];// }// if ([elementName isEqualToString:@"image"]) {// [self.video setValue:self.currentElementString forKey:elementName];// }// if ([elementName isEqualToString:@"length"]) {// [self.video setValue:self.currentElementString forKey:elementName];// }// if ([elementName isEqualToString:@"url"]) {// [self.video setValue:self.currentElementString forKey:elementName];// }// if ([elementName isEqualToString:@"name"]) {// [self.video setValue:self.currentElementString forKey:elementName];// } if (![elementName isEqualToString:@"vedio"] && ![elementName isEqualToString:@"vedios"]) { [self.video setValue:self.currentElementString forKey:elementName]; } // 将模型添加到数据源中. if ([elementName isEqualToString:@"vedio"]) { [self.videos addObject:self.video]; } }- (void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"5.XML文档解析结束! %@",[NSThread currentThread]); // 可以在这里刷新数据,但是注意线程问题!}
0 0
- IOS XML解析解析方法SAX
- iOS Objective-C 解析XML SAX解析方法
- IOS XML的SAX解析
- SAX中止解析XML方法
- XML文件解析---SAX方法
- sax方法进行xml解析
- SAX方法解析XML文件
- IOS XML 解析SAX解析方式
- iOS:XML文件的解析(SAX解析)
- XML解析(SAX解析)
- XML解析:SAX解析
- [XML] SAX解析XML
- 【XML】SAX解析XML
- ios随笔之SAX解析XML文件
- iOS用SAX方式解析XML
- iOS SAX 、DOM 、XML解析区别笔记
- Java 解析xml的方法:DOM,SAX
- SAX方式解析XML的简单方法
- iOS之百度地图获取地理位置名称
- docker 数据管理<1>
- 微信统一订单
- 新手学习记录3:makefile学习实践--自动变量引入
- 连号区间数
- IOS XML解析解析方法SAX
- 栈的应用(一)——中缀表达式转后缀表达式
- 《战拖有术》读书笔记(I)
- 蓝桥杯-兰顿蚂蚁
- Arduino通过串口发送结构体
- Java获取网络终端连接网络类型
- c#学习
- spring中xml转化成对应bean的几种方式
- Hdu 2031 (进制转换)