您的位置:首页 > 理论基础 > 数据结构算法

iOS编程------XML、JSON数据结构解析

2015-10-05 21:34 417 查看
//
//  AppDelegate.h
//  UI15_xml_json
//
//  Created by l on 15/9/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

//
//  AppDelegate.m
//  UI15_xml_json
//
//  Created by l on 15/9/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "GDataXMLNode.h"
#import "Person.h" //model
@interface AppDelegate ()
@property (nonatomic, strong) NSMutableArray *modelArray;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// UI15 数据解析
//一. XML 解析
//主要采用DOM方式解析,需要导入Google的第三方类库
//导入注意点:
//1.导入动态链接库 libxml2
//2.指定头文件路径 /usr/include/libxml2
//3.mrc  arc  混合编译
//mrc 文件 -fno-objc-arc
//arc 文件 -f-objc-arc

//一.xml解析
//1.文件路径
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"xml文件" ofType:@"xml"];
//2.取出文件内容,字符串接收
NSString *xmlString = [NSString stringWithContentsOfFile:xmlFilePath encoding:NSUTF8StringEncoding error:nil];
//3.xml解析,把内容转化为document,树结构存储
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
//4.获取文档的根节点,使用rootElement属性
GDataXMLElement *rootElement = document.rootElement;

NSLog(@"%@", document);
NSLog(@"%@", [rootElement stringValue]);

//5.寻找子节点有三种方式
//(1)寻找节点的第二种方式, children 使用children属性
//获取一个节点的所有子节点
NSArray *students = [rootElement children];

//获取数组中第一个节点
GDataXMLElement *liuruichao = students[0];
NSLog(@"%@", [liuruichao stringValue]);

//获取姓名,性别,年龄
NSArray *propertys = [liuruichao children];

for (GDataXMLElement *ele in propertys) {
NSLog(@"%@", [ele stringValue]);
}

//(2)寻找节点的第二种方式,根据节点名去寻找
//注意: 不能越级查找
//ElementForName
NSArray *students2 = [rootElement elementsForName:@"student"];
NSLog(@"%@", [students2[1] stringValue]);

GDataXMLElement *name = [students2[1] elementsForName:@"name"][0];
NSLog(@"%@", [name stringValue]);

//(3)寻找节点的第三种方式,根据节点路径去查找
//nodesForPath: error:
//path 节点路径
NSArray *nameArray = [rootElement nodesForXPath:@"student/name" error:nil];
for (GDataXMLElement *name in nameArray) {
NSLog(@"%@", [name stringValue]);

}

/*    GDataXMLElement *yinlei = [rootElement nodesForXPath:@"student" error:nil][2];
NSLog(@"%@", [yinlei stringValue]);
*/

//总结: xml数据解析
//1. 一个document 一个文档对象,GDataXMLDocument alloc initWithXmlString:

//2. 2类节点 根节点
//rootElement = document.rootElement;
//子节点

//3.3种子节点的查找方式
// children       获取所有子节点
//elementForName  获取同名的子节点
//nodesForXPath   获取同路径的子节点

//练习1.分别打印出 xml中学生的信息.
/* NSArray *studentsInfo = [rootElement children];
for (GDataXMLElement *student in studentsInfo) {
NSArray *pro = [student children];
for (GDataXMLElement *ele1 in pro) {
NSLog(@"%@", [ele1 stringValue]);
}
}  */

//1.根节点 rootElement
//2.分别取出来每一个学生节点
//    NSArray *studentArray2 = [rootElement children];
//    for (GDataXMLElement *student in studentArray2) {
//        for (GDataXMLElement *property in [student children]) {
//            NSLog(@"%@", [property stringValue]);
//        }
//    }

//-----------------------------//

//二. json 解析
//json 解析和plist 文件解析很类似,都是根据根节点创建对应的数据容器,不同的是json从data中解析数据

//解析本地数据
//1.获取本地json文件路径
NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:@"person" ofType:@"json"];
//2.获取data对象,把本地文件转化为data对象
NSData *data = [NSData dataWithContentsOfFile:jsonFilePath];
//3.使用系统自带的json进行解析
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

//数据容器初始化
self.modelArray = [NSMutableArray array];

for (NSDictionary *dic in jsonArray) {
//        NSLog(@"%@", dic);

//创建model对象
Person *person = [[Person alloc] init];
//kvc赋值
[person setValuesForKeysWithDictionary:dic];
//添加
[_modelArray addObject:person];

}

NSLog(@"%@", _modelArray);

return YES;
}

@end

///////////////////////////////////////////////////////////////////

//
//  Person.h
//  UI15_xml_json
//
//  Created by l on 15/9/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *age;

@end

//
//  Person.m
//  UI15_xml_json
//
//  Created by l on 15/9/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "Person.h"

@implementation Person

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

//
}

@end

////////////////////////////////////////////////////////////////////

//
//  ViewController.h
//  UI15_xml_json
//
//  Created by l on 15/9/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//
//  ViewController.m
//  UI15_xml_json
//
//  Created by l on 15/9/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: