标签:
- (void)viewDidLoad {
[super viewDidLoad];
// 获得服务器数据.
[self getServerDataWithUrlString:@"http://localhost/resources/vedios2.xml"];
}
- (void)getServerDataWithUrlString:(NSString *)urlString
{
// 1. 创建请求
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 处理数据
// NSLog(@"data :%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
// DOM 解析
[self XMLDomWithData:data];
// 刷新数据
[self.tableView reloadData];
}];
}
// DOM解析.
- (void)XMLDomWithData:(NSData *)data
{
// 将XML文档加载进来
GDataXMLDocument *dom = [[GDataXMLDocument alloc] initWithData:data options:0 error:NULL];
// 得到根元素的内容
GDataXMLElement *rootElement = dom.rootElement;
// NSLog(@"%@",rootElement.children);
// 遍历跟元素中的子元素
[rootElement.children enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// 实例化数据模型
CZVideo *video = [[CZVideo alloc] init];
// 获得 vedio 元素
GDataXMLElement *vedioElement = obj;
// NSLog(@"%@",vedioElement.children);
// 遍历 vedio 元素中的子元素
[vedioElement.children enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//
GDataXMLElement *element = obj;
NSLog(@"%@ %@",element.name, element.stringValue);
[video setValue:element.stringValue forKey:element.name];
}];
// 将数据模型添加到数据源中
[self.videos addObject:video];
}];
// 刷新数据
[self.tableView reloadData];
}
标签:
原文地址:http://www.cnblogs.com/xhc1263478959/p/4808064.html