1 // 2 // HVWVideoViewController.m 3 // ParseXMLDemo 4 // 5 // Created by hellovoidworld on 15/1/26. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWVideoViewController.h" 10 #import "HVWVideo.h" 11 #import "HVWVideoCell.h" 12 13 @interface HVWVideoViewController () <UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate, NSXMLParserDelegate> 14 15 /** 缓存数据 */ 16 @PRoperty(nonatomic, strong) NSMutableData *data; 17 18 /** 缓存视频信息 */ 19 @property(nonatomic, strong) NSMutableArray *videos; 20 21 @end 22 23 @implementation HVWVideoViewController 24 25 - (void)viewDidLoad { 26 [super viewDidLoad]; 27 28 // 发送请求 29 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/video?type=xml"]; 30 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 31 NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; 32 [conn start]; 33 } 34 35 #pragma mark - Table view data source 36 37 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 38 return 1; 39 } 40 41 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 42 return self.videos.count; 43 } 44 45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 46 HVWVideoCell *cell = [HVWVideoCell cellWithTableView:tableView]; 47 cell.video = self.videos[indexPath.row]; 48 return cell; 49 } 50 51 #pragma mark - UITableViewDelegate 52 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 53 return 70; 54 } 55 56 57 #pragma mark - NSURLConnectionDataDelegate 58 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 59 NSLog(@"网络繁忙,请稍后再试!"); 60 } 61 62 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 63 NSLog(@"开始接受数据"); 64 self.data = [NSMutableData data]; 65 } 66 67 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 68 NSLog(@"正在接受数据 ---> 数据长度:%d byte", data.length); 69 70 // 累加数据 71 [self.data appendData:data]; 72 } 73 74 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 75 NSLog(@"完成下载数据"); 76 77 // 开始处理数据 78 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.data]; 79 parser.delegate = self; 80 // 开始解析 81 [parser parse]; 82 } 83 84 #pragma mark - NSXMLParserDelegate 85 /** xml文档开始 */ 86 - (void)parserDidStartDocument:(NSXMLParser *)parser { 87 NSLog(@"开始解析xml"); 88 } 89 90 /** xml文档结束 */ 91 - (void)parserDidEndDocument:(NSXMLParser *)parser { 92 NSLog(@"解析xml完毕"); 93 } 94 95 /** 开始解析元素 */ 96 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 97 NSLog(@"开始解析 %@", elementName); 98 99 if ([@"videos" isEqualToString:elementName]) { // 这个也会解析到根节点 100 self.videos = [NSMutableArray array]; 101 } else if ([@"video" isEqualToString:elementName]) { // 如果是视频节点,开始提取 102 HVWVideo *video = [HVWVideo videoWithDict:attributeDict]; 103 [self.videos addObject:video]; 104 } 105 } 106 107 /** 结束解析元素 */ 108 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 109 NSLog(@"解析完毕 %@", elementName); 110 111 // 刷新数据 112 [self.tableView reloadData]; 113 } 114 115 @endc.GDataXML配置 GDataXML基于libxml2库,得做以下配置 导入libxml2库
1 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 2 NSLog(@"完成下载数据"); 3 4 // 开始处理数据 5 // 1.加载文档 6 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:self.data options:0 error:nil]; 7 8 // 2.获得根元素 9 GDataXMLElement *root = [doc rootElement]; 10 11 // 3.解析出所有元素 12 NSArray *elements = [root elementsForName:@"video"]; 13 14 // 4.封装数据 15 self.videos = [NSMutableArray array]; 16 for (GDataXMLElement *ele in elements) { 17 HVWVideo *video = [[HVWVideo alloc] init]; 18 video.name = [ele attributeForName:@"name"].stringValue; 19 video.length = [ele attributeForName:@"length"].stringValue; 20 video.image = [ele attributeForName:@"image"].stringValue; 21 video.video = [ele attributeForName:@"video"].stringValue; 22 [self.videos addObject:video]; 23 } 24 25 // 刷新数据 26 [self.tableView reloadData]; 27 }