zoukankan      html  css  js  c++  java
  • XML解析之DOM详解及与SAX解析方法的比较

    XML解析(DOM)

    XML文件解析方法介绍

    我们所用到的NSXMLParser是采用SAX方法解析

    SAX(Simple API for XML)
    • 只能读,不能修改,只能顺序访问,适合解析大型XML,解析速度快
    • 常应用于处理大量数据的XML,实现异构系统的数据访问,实现跨平台
    • 从文档的开始通过每一节点移动,定位一个特定的节点
    DOM(Document Object Model)
    • 不仅能读,还能修改,而且能够实现随机访问,缺点是解析速度慢,适合解析小型文档
    • 一般应用与小型的配置XML,方便操作
    • 为载入到内存的文档节点建立类型描述,呈现可横向移动、潜在巨大的树型结构
    • 在内存中生成节点树操作代价昂贵

     NSXMLParser解析

    1.创建NSXMLParser实例,并传入从服务器接收的XML数据
     
    2.定义解析器代理
     
    3.解析器解析
     
    4.通过解析代理方法完成XML数据的解析
     
    使用XML解析文档时使用协议<NSXMLParserDelegate>,实现它的代理方法

     1. 开始解析某个元素,会遍历整个XML,识别元素节点名称

    - (void)parser:didStartElement:namespaceURI:qualifiedName:attributes:

     2. 文本节点,得到文本节点里存储的信息数据,对于大数据可能会接收多次!为了节约内存开销

    - (void)parser:foundCharacters:

     3. 结束某个节点,存储从parser:foundCharacters:方法中获取到的信息

    - (void)parser:didEndElement:namespaceURI:qualifiedName:

    注意:在解析过程中,上述三个方法会不停的重复执行,直到遍历完成为止

     4. 开始解析XML文档

    - (void)parserDidStartDocument:

     5. 解析XML文档结束

    - (void)parserDidEndDocument:

     6. 解析出错

    - (void)parser:parseErrorOccurred:
     
    在XMLParser解析过程中,还需要实现NSXMLParserDelegate代理方法
    如果一个XML文件中包含多个对象在解析过程中,为了能够正确解析中文档中的数据,需要注意一下几点:
    1.当前解析出得是对象还是元素值?
    如果是对象,需要判断当前对象时第几个,是第一个对象,还是第二、第三……,如果是第N个,需要将第N-1的对象值取出保存。
    如果是元素值,需要将解析出得数据,赋值给对应于对象的某个属性。
    2.在解析过程中,每次读取的是一个字符,所有必须实现字符串的拼接过程,将解析出字符进行组合。用来判断当前解析出得是对象,还是元素名。或元素值。
     

    XML(DOM)具体步骤

    1.在github上下载GDataXMLNode

    2.配置第三方

    3.使用GDataXMLNode解析XML数据

    - 下载大家应该都会把,不用我教,直接来第二步

    点击工程名字,然后如下图所示添加代码,然后我们的第三方库就可以使用了

    第三步:进行XML解析数据-这儿直接上代码,注释也挺详细的,相信你们都可以看懂

    viewCOntroller

      1 //
      2 //  ViewController.m
      3 //  XML(DOM解析)
      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 "GDataXMLNode.h"
     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.xml"];
     85     
     86     NSURLRequest *requst = [NSURLRequest requestWithURL:url];
     87     
     88     [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
     89         
     90         //根据数据生成xml解析文档
     91         GDataXMLDocument *gData = [[GDataXMLDocument alloc]initWithData:data error:nil];
     92         //获取xml根节点下所有子节点
     93         for (GDataXMLElement *element in gData.rootElement.children) {
     94             
     95             //获取子节点下的孙子节点
     96             for (GDataXMLElement * trainInfo in element.children) {
     97                 
     98                 //创建模型
     99                 JKPTrainInfo *info = [[JKPTrainInfo alloc]init];
    100                 
    101                 // 经检测 stringValue是数据 name是节点的名字  GDataXMLElement是节点下的数据部分
    102     
    103                 //即这个<trainStation>上海南(车次:T81T80)</trainStation>
    104                 for (GDataXMLElement *note in 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                 //GDataXMLNode *att 是XML文件中这个 <trainDetailInfo info="trainDetailInfo1" rowOrder="0" hasChanges="inserted">
    116                 for (GDataXMLNode *att in 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)initUI
    140 {
    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
    //  XML解析(SAX)练习
    //
    //  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
  • 相关阅读:
    poj 最长公共子序列 1458 记忆式搜索
    选择排序
    直接 插入排序
    直接插入排序
    洛谷-P3389-高斯消元
    经济中的哪些概念
    uva-622-dp
    UVA-607-DP
    转转---面试题
    Linux事件驱动IO中select vs epoll
  • 原文地址:https://www.cnblogs.com/ldnh/p/5295070.html
Copyright © 2011-2022 走看看