我们所用到的NS
SAX(Simple API for
- 只能读,不能修改,只能顺序访问,适合解析大型
- 常应用于处理大量数据的
- 从文档的开始通过每一节点移动,定位一个特定的节点
DOM(Document Object Model)
- 不仅能读,还能修改,而且能够实现随机访问,缺点是解析速度慢,适合解析小型文档
- 一般应用与小型的配置
- 为载入到内存的文档节点建立类型描述,呈现可横向移动、潜在巨大的树型结构
- 在内存中生成节点树操作代价昂贵
NS
1.创建NS
2.定义解析器代理
3.解析器解析
4.通过解析代理方法完成
使用
1. 开始解析某个元素,会遍历整个
- (void)parser:didStartElement:namespaceURI:qualifiedName:attributes:
2. 文本节点,得到文本节点里存储的信息数据,对于大数据可能会接收多次!为了节约内存开销
- (void)parser:foundCharacters:
3. 结束某个节点,存储从parser:foundCharacters:方法中获取到的信息
- (void)parser:didEndElement:namespaceURI:qualifiedName:
注意:在解析过程中,上述三个方**不停的重复执行,直到遍历完成为止
4. 开始解析
- (void)parserDidStartDocument:
5. 解析
- (void)parserDidEndDocument:
6. 解析出错
- (void)parser:parseErrorOccurred:
在
如果一个
1.当前解析出得是对象还是元素值?
如果是对象,需要判断当前对象时第几个,是第一个对象,还是第二、第三……,如果是第N个,需要将第N-1的对象值取出保存。
如果是元素值,需要将解析出得数据,赋值给对应于对象的某个属性。
2.在解析过程中,每次读取的是一个字符,所有必须实现字符串的拼接过程,将解析出字符进行组合。用来判断当前解析出得是对象,还是元素名。或元素值。
1.在github上下载GData
2.配置第三方
3.使用GData
- 下载大家应该都会把,不用我教,直接来第二步
点击工程名字,然后如下图所示添加代码,然后我们的第三方库就可以使用了
images/loading.gif' data-original="http://images2015.cnblogs.com/blog/910545/201603/910545-20160319134814896-506101931.png" width="700" height="424" />
第三步:进行
viewCOntroller
1 // 2 // ViewController.m 3 // 4 // 5 // Created by ma c on 16/3/19. 6 // Copyright (c) 2016年 姬凯鹏. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "JKPTrainInfo.h" 11 #import "GData" 12 @interface ViewController ()<UITableViewDataSource> 13 14 @property (nonatomic, strong) NSMutableArray * dataList; 15 16 @property (nonatomic,strong) UITableView *tableView; 17 18 @end 19 20 @implementation ViewController 21 22 - (void)viewDidLoad { 23 [super viewDidLoad]; 24 // Do any additional setup after loading the view, typically from a nib. 25 26 [self initUI]; 27 28 [self loadData]; 29 } 30 31 32 #pragma mark -getter and setter methods 33 34 - (NSMutableArray *)dataList { 35 36 if (!_dataList) { 37 _dataList = [NSMutableArray array]; 38 } 39 return _dataList; 40 } 41 42 -(UITableView *)tableView 43 { 44 if (!_tableView) { 45 46 _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; 47 48 _tableView.dataSource = self; 49 } 50 return _tableView; 51 } 52 53 #pragma mark - UITableView dataSource 54 55 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 56 57 return self.dataList.count; 58 } 59 60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 61 62 static NSString * identifiter = @"cellID"; 63 64 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifiter]; 65 66 if (!cell) { 67 68 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifiter]; 69 } 70 JKPTrainInfo *JkpInfo = self.dataList[indexPath.row]; 71 72 cell.textLabel.text = JkpInfo.trainStation; 73 74 cell.detailTextLabel.text = JkpInfo.arriveTime; 75 76 return cell; 77 } 78 79 80 #pragma mark - event handle methods 81 // 载入数据 82 - (void)loadData 83 { 84 NSURL *url = [NSURL URLWithString:@"http://192.168.1.68/train."]; 85 86 NSURLRequest *requst = [NSURLRequest requestWithURL:url]; 87 88 [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 89 90 //根据数据生成 91 GData [[GData 92 //获取 93 for (GDatain gData.rootElement.children) { 94 95 //获取子节点下的孙子节点 96 for (GDatain element.children) { 97 98 //创建模型 99 JKPTrainInfo *info = [[JKPTrainInfo alloc]init];100 101 // 经检测 stringValue是数据 name是节点的名字 GData102 103 //即这个<trainStation>上海南(车次:T81\T80)</trainStation>104 for (GDatain trainInfo.children) {105 106 [info setValue:note.stringValue forKey:note.name];107 108 // NSLog(@"%@",note.stringValue);109 // 110 // NSLog(@"-----------");111 // 112 // NSLog(@"%@",note.name);113 }114 115 //GData116 for (GDatain trainInfo.attributes ) {117 118 [info setValue:att.stringValue forKey:att.name];119 120 NSLog(@"%@",att.stringValue);121 122 NSLog(@"-----------");123 124 NSLog(@"%@",att.name);125 126 }127 //将模型加入到数据中128 [self.dataList addObject:info];129 }130 131 }132 //刷新数据 一定要在异步请求里面刷新数据 不然不好使133 [self.tableView reloadData];134 135 }];136 }137 138 //载入视图139 - (void)initUI140 {141 [self.view addSubview:self.tableView];142 }143 - (void)didReceiveMemoryWarning {144 [super didReceiveMemoryWarning];145 // Dispose of any resources that can be recreated.146 }147 148 @end
mode类
//// JKPTrainInfo.h// //// Created by ma c on 16/3/19.// Copyright (c) 2016年 姬凯鹏. All rights reserved.//#import <Foundation/Foundation.h>@interface JKPTrainInfo : NSObject//解析数据@property (nonatomic, copy) NSString * info;@property (nonatomic, copy) NSString * rowOrder;@property (nonatomic, copy) NSString * hasChanges;@property (nonatomic, copy) NSString * trainStation;@property (nonatomic, copy) NSString * arriveTime;@property (nonatomic, copy) NSString * startTime;@property (nonatomic, copy) NSString * KM;@end
原标题:XML解析之DOM详解及与SAX解析方法的比较
关键词:xml