DOM数据解析
2015-09-14 19:58
218 查看
- (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];
}
[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];
}
相关文章推荐
- [置顶] android studio 查看源码时有些类显示 can not resolve 的问题
- SDWebImage的简单使用
- java中的SecureRandom在linux中的实现
- vlc 播放器的播放进度条的时间不刷新
- hdu 5437 Alisha’s Party (线段树)
- 【Linux命令详解】12、文本内容显示―[ cat、tac、more、less、tail、head]
- 黑马程序员-------C语言----文件读写
- 设计模式——代理模式(RMI初接触)
- XML数据解析可以直接调用此方法
- 第三周—项目2 建设“顺序表”算法库
- 取随机数不重复问题
- JAVA连接mysql
- Objective-C 【NSDictionary 文件操作(重点)&简写形式】
- C++内存管理
- ireport 动态设置属性
- 归并排序c++实现
- iOS计算缓存大小、清除缓存
- 程序员收集的书单
- eclipse 配置黑色主题
- npp插件-NewFileBrowser:自定义模板