xml、和json因两种方式解析数据data

下边是两种对数据的解析  xml、和json因两种方式都比较简单 所以直接附上源码 相信能一眼读懂

 

json的是系统的

 

id dat=[NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];

其中data是下载下来的数据  

options

typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {

    NSJSONReadingMutableContainers = (1UL << 0),

    NSJSONReadingMutableLeaves = (1UL << 1),

    NSJSONReadingAllowFragments = (1UL << 2)

};

 

NSJSONReadingMutableContainers  是返回数组或者词典

NSJSONReadingMutableLeaves  可变字符串对象

操作:

 id dat=[NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];

//或者isKindOfClass  [NSArrayclass]

    if ([dat isKindOfClass:[NSDictionary class]]) {

        NSArray * array=[dat objectForKey:@"users"];

        [_dataArrayaddObjectsFromArray:array];

    }

之后就是对数组的处理了

 

 

xml方法对数据进行处理  使用xml需要使用GDataXMLNode的第三方库

//常规的解析xml的方法

- (void)parsingXml{

    //获取txt路径

    NSString *path = [[NSBundlemainBundle] pathForResource:@"xml"ofType:@"txt"];

    //读取txt中的数据

    NSString *xmlString = [NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

    //GDataXMLDocument 相当于存放xml数据的容器,初始化的时候,initWithXMLString xml的字符串传递给GDataXMLDocument的对象,后续的解析操作通过doc来完成

    //options 无用

    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];

    //node是节点

    NSArray *myNames = [doc nodesForXPath:@"//name" error:nil];

//    NSLog(@"namesCount:%@",myNames);

 

    //xml数据是由多个节点构成的

    //xml中任何一个节点(无论根节点、父节点、子节点)都是一个GDataXMLElement的对象

    //取到根节点

    GDataXMLElement *root = [doc rootElement];

//    NSLog(@"root:%@",root);

    //取到root 标题名称为books的子节点

    //返回值为带有子节点的数组,数组中存有一个或多个GDataXMLElement节点对象

    NSArray *array =[root elementsForName:@"books"];

    //从数组中取出books节点

    GDataXMLElement *books = [array objectAtIndex:0];

    //books下面的book节点

    NSArray *bookArray = [books elementsForName:@"book"];

//    NSLog(@"book:%d",bookArray.count);

    //遍历数组

    for (GDataXMLElement *book in bookArray) {

        //book下,标题为name的子节点

       NSArray *nameArray =[book elementsForName:@"name"];

        GDataXMLElement *name = [nameArray objectAtIndex:0];

        //获取name节点的内容:(通过节点的stringValue属性获取节点的内容)

        NSString *nameValue = name.stringValue;

//        NSLog(@"name:%@",nameValue);

        //获取节点的标题(通过节点的name属性获取)

//        NSLog(@"%@",name.name);

        //获取整个节点的字符串 <name>..</name>,通过XMLString属性

//        NSLog(@"xmlString:%@",name.XMLString);

        

        //如何获取book节点的属性/通过attributes获取节点的所有属性

        //属性也是节点的一种,每一个属性也是一个GDataXMLElement对象,attributes存有所有属性对象的数组

        NSArray *attArray =book.attributes;

        //取到id属性

        GDataXMLElement *bookId = [attArray objectAtIndex:0];

        //取到属性的值

        NSLog(@"bookId:%@",bookId.stringValue);

        GDataXMLElement *la = [attArray objectAtIndex:1];

        NSLog(@"la:%@",la.stringValue);

    }

}

 

//xpath语句解析xml(xpath 是对xml数据的节点进查找的语言,查找是通过xpath语句第二种方法可以使用节点的方法

- (void)xpath{

   //通过节点在xml数据中的绝对位置来取到该节点

   //      /root/user_list/user

    NSString *path = [[NSBundlemainBundle] pathForResource:@"sns"ofType:@"txt"];

    NSString *xmlString = [NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];

    //通过xpath语句,取到user节点   /root/user_list/user 是一条xpath语句,代表userxml中的绝对位置

     NSArray *users =[doc nodesForXPath:@"/root/user_list/user" error:nil];

    for (GDataXMLElement *user in users) {

        NSArray *userNames = [user elementsForName:@"username"];

        //获取username

        GDataXMLElement *userName = [userNames objectAtIndex:0];

//        NSLog(@"name:%@",userName.stringValue);

    }

    //   //+节点标题(totalcount) 取到标题为totalcount的所有节点,不管节点在xml中的什么位置

    NSArray *array = [doc nodesForXPath:@"//totalcount" error:nil];

    GDataXMLElement *total = [array objectAtIndex:0];

    NSLog(@"totalCount:%@",total.stringValue);

}

 

 

原文地址:https://www.cnblogs.com/AbelChen1991/p/3602275.html