#import "LKViewController.h"
#import "Video.h"
@interface LKViewController () <NSXMLParserDelegate>
// 表格的数据列表
@property (nonatomic,strong) NSArray *dataList;
// 最终的数组
@property (nonatomic,strong) NSMutableArray *videosM;
// 拼接元素内容的字符串
@property (nonatomic,strong) NSMutableString *elementM;
// 当前正在处理的节点
@property (nonatomic,strong) Video *currentVideo;
@end
@implementation LKViewController
- (NSMutableArray *)videosM
{
if (!_videosM) _videosM = [NSMutableArray array];
return _videosM;
}
- (NSMutableString *)elementM
{
if (!_elementM) _elementM = [NSMutableString string];
return _elementM;
}
- (void)setDataList:(NSArray *)dataList
{
_dataList = dataList;
// 刷新控件完成刷新
[self.refreshControl endRefreshing];
[self.tableView reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadData];
}
#pragma mark - 表格数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
cell.textLabel.text = [self.dataList[indexPath.row] name];
return cell;
}
#pragma mark - XML解析
/** 加载数据 */
- (IBAction)loadData
{
// 1. URL
NSURL *url = [NSURL URLWithString:@"http://192.168.26.3/videos.xml"];
// 2. Request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 异步方法
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// XML解析
// 1> 实例化解析器
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 2> 设置代理
parser.delegate = self;
// 3> 解析器开始解析
[parser parse];
}];
}
#pragma mark - NSXMLParserDelegate
// 1. 开始文档
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(@"开始文档");
// 初始化准备,清空数组
[self.videosM removeAllObjects];
}
// 2. 开始节点
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"%@ %@", elementName, attributeDict);
// 如果是video,创建Video对象,准备设置内容,顺便设置videoId
if ([elementName isEqualToString:@"video"]) {
// 实例化对象
self.currentVideo = [[Video alloc] init];
// 设置videoId
[self.currentVideo setValue:attributeDict[@"videoId"] forKeyPath:@"videoId"];
}
// 要拼接的字符串清空
[self.elementM setString:@""];
}
// 3. 发现节点文本
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"%@", string);
// 拼接字符串
[self.elementM appendString:string];
}
// 4. 结束节点
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"结束节点 %@", elementName);
// 如果是/video,第二步实例化的video添加到数组
if ([elementName isEqualToString:@"video"]) {
[self.videosM addObject:self.currentVideo];
} else if (![elementName isEqualToString:@"videos"]) {
[self.currentVideo setValue:self.elementM forKeyPath:elementName];
}
}
// 5. 结束文档
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"结束文档 %@",self.videosM);
self.dataList = self.videosM;
}
// 6. 出错方法
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%@", parseError.localizedDescription);
}
@end