• iOS之解析


    解析:从事先规定好的格式中提取数据

    前提:提前约定好格式。

    XML解析

    DOM :Document Object Model解析  

    原理: 一次性读入整个XML  以栈的方式解析每一个标签,开标签入栈 关标签出栈 当栈中没有任何元素的时候解析结束  

    优点:  一次解析出全部数据 而且有明显的层级关系

    缺点:当XML太大的时候特别占内存

    解决:将大的XML分成多个小的XML

    SAX:Simple API for XML 解析 基于事件驱动的解析方式,逐行解析数据(采用协议回调机制)

    优势:逐行解析,不需要读入整个XML因此更节省内存空间  因为是基于回调的解析方式,所以用户可以更加灵活的控制解析过程

    缺点:需要用户自己去处理数据 

    NSXMLParser是IOS的XML解析类,采用sax方式解析数据 (不常用,几乎不用)

    GDataXMLNode是googol提供的开源XML解析类 采用DOM方式解析数据(常用此方式解析)

    使用流程:1 将GDataxMLNode.h和GDataXMLNode.m文件加入到项目中        

            2 进入Xcode  加入libxml.dylib类库 )(工程 - build Phases 添加上类库 )    

         

            3  找到Search Paths段,在Header Search Paths添加值为:/usr/include/libxml2

     

     

     

    NSString *xmlFilePath = [[NSBundlemainBundle] pathForResource:@"Student"ofType:@"xml"];

        NSData *data = [NSData dataWithContentsOfFile:xmlFilePath];

        

        GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; // 解析完毕

        NSLog(@"%@", document.rootElement);

        

        

        // xPath

        // 相对路径使用//表示相对路径

        // 取出节点中所有叫 name 的节点....

        NSArray *names = [document nodesForXPath:@"//student/name" error:nil];

        NSArray *sexs = [document nodesForXPath:@"//student/sex" error:nil];

        NSArray *ages = [document nodesForXPath:@"//student/age" error:nil];

           

        //绝对路径要给出完整路径  / 表示绝对路径

        NSArray *sex = [document nodesForXPath:@"/students/student/sex" error:nil];

     

    JSON解析

    javascripe object na

    1 json 格式要正确 最后一个元素后面没有逗号“,”

    - (id)objectFromJSONString 将字符串解析成指定对象

    - (id)objectFromJSONData 将data数据解析成指定对象

     /**

         *  JSON解析 

         * 1 创建(或导入).json文件

         * 2 获取文件路径  NSString *filePath = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"json"]

         * 3 得到文件中的数据  NSString NSData

         * 4 解析 a 使用第三方工具JSONKit b 使用 NSJSONSerialization

         */

        

        NSString *filePath = [[NSBundlemainBundle] pathForResource:@"Person"ofType:@"json"];

     

        //解析

        // 使用第三放解析工具 JSONKit

        // 通过字符串

        NSString *JSONStr = [NSStringstringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding  error:nil];

        NSDictionary *JSONDic = [JSONStr objectFromJSONString];

        

        // 通过data数据

        NSData *JSONData = [NSData dataWithContentsOfFile:filePath];

        NSDictionary *JSONDic2 = [JSONData objectFromJSONData];

        

        // 使用苹果自带的解析类 NSJSONSerialization

        NSDictionary *JSONDic3 = [NSJSONSerializationJSONObjectWithData:JSONData options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];

        

    //    NSString --> NSData

        NSString *theStr = @"Hello Lanou";

        NSData *theData = [theStr dataUsingEncoding:NSUTF8StringEncoding];

          

    //    NSData --> NSString

        NSString *string = [[NSStringalloc] initWithData:theData encoding:NSUTF8StringEncoding];

  • 相关阅读:
    日志
    设置和开启定时器
    缓存管理
    计算机程序员能做多久,这个行业有年龄限制吗?
    程序员都是怎么工作的?
    做程序员怎么样?
    javascript中this关键字
    1003. 二哥养细菌—java
    1002. 二哥种花生——java
    this与static
  • 原文地址:https://www.cnblogs.com/NatureZhang/p/3718474.html
Copyright © 2020-2023  润新知