您的位置:首页 > 产品设计 > UI/UE

IOS_UI_数据解析

2015-09-09 16:52 399 查看
SAX解析 json解析

代码和详细解释如下:

#import <UIKit/UIKit.h>

@interface AppDelegate :
UIResponder <UIApplicationDelegate>

@property (strong,
nonatomic) UIWindow *window;

@end

#import "AppDelegate.h"

#import "MainViewController.h"

@interface
AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc

{

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor
whiteColor];

[self.window
makeKeyAndVisible];

[_window release];

MainViewController *mainVC = [[MainViewController
alloc]init];

self.window.rootViewController = mainVC;

[mainVC release];

return
YES;

}

#import <UIKit/UIKit.h>

@interface MainViewController :
UIViewController

@end

#import "MainViewController.h"

#import "Student.h"

@interface
MainViewController ()<NSXMLParserDelegate>

//1

@property (nonatomic ,retain)NSMutableArray *arr;

@property (nonatomic,retain)NSString *tempStr;

@end

@implementation MainViewController

//2

- (void)dealloc

{

[_arr release];

[_tempStr release];

[super dealloc];

}

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

// //从文件中读取字符串 取得名字

// NSString *path = [[NSBundle mainBundle ]pathForResource:@"Student" ofType:@"xml"];

// //从路径中取得文件内容

// NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// NSLog(@"%@",str);

//SAX解析 调用 1.

[self
xmlSAXParse];

}

//自己写的三个方法

- (void)xmlSAXParse//逐行解析

{

//获取路径

NSString *path = [[NSBundle
mainBundle ]pathForResource:@"Student"
ofType:@"xml"];

//从路径中取得文件内容

NSString *str = [NSString
stringWithContentsOfFile:path encoding:NSUTF8StringEncoding
error:nil];

NSLog(@"%@",str);

//SAX解析XML是以逐行读取的方式解析的

// 系统提供的一个类:NSXMLParser

//通过路径获取数据

NSData *data = [NSData
dataWithContentsOfFile:path];

//通过数据(NSData)创建解析

NSXMLParser *parser = [[NSXMLParser
alloc]initWithData:data];

//设置代理

parser.delegate =
self;

//开始解析

[parser parse];

//JSon解析调用

[self
jsonParse];

}

//协议方法

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString
*)qName attributes:(NSDictionary *)attributeDict

{

//发现节点头

NSLog(@"%@",elementName);

//3

if ([elementName
isEqualToString:@"students"]) {

self.arr = [NSMutableArray
array];

}else if ([elementName
isEqualToString:@"student"]){

//当遇到student节点,就创建一个student对象添加到数组中

Student *stu = [[Student
alloc]init];

[self.arr
addObject:stu];

[stu release];

}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

//发现节点内容

NSLog(@"%@",string);

//4 保存一下节点内容 方便结尾的时候赋值

self.tempStr = string;

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString
*)qName

{

//发现节点尾

NSLog(@"%@",elementName);

Student *stu = [self.arr
lastObject];

// if ([elementName isEqualToString:@"name"]) {

// stu.name = self.tempStr;

// }else if ([elementName isEqualToString:@"number"]){

// stu.number = self.tempStr;

// }else if ([elementName isEqualToString:@"sex"]){

// stu.sex = self.tempStr;

// }else if ([elementName isEqualToString:@"phone"]){

// stu.phone = self.tempStr;

// }

//KVC 写法 前提是student类里纠错方法都写上

[stu setValue:self.tempStr
forKey:elementName];

}

- (void)xmlDOMParse//按节点解析

{

}

- (void)jsonParse

{

//json解析

NSString *path = [[NSBundle
mainBundle]pathForResource:@"Student"
ofType:@"txt"];

//从路径读取数据

NSData *data = [NSData
dataWithContentsOfFile:path];

//参数1 : 需要JSon 解析的数据

//参数2 : 解析的结果设置

// id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSArray * result = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];

NSLog(@"%@",result);

}

#import <Foundation/Foundation.h>

@interface Student :
NSObject

@property (nonatomic,retain)NSString *name;

@property (nonatomic,retain)NSString *sex;

@property (nonatomic,retain)NSString *phone;

@property (nonatomic,retain)NSString *number;

@end

//<students>创建数组

//<student>创建student对象

//<name>张扬</name>

//<sex>男</sex>

//<phone>110</phone>

//</student>

#import "Student.h"

@implementation Student

- (void)setValue:(id)value forKey:(NSString *)key

{

//这是一个使用kvc赋值的纠错方法,用于处理没有定义的key

}

- (id)valueForUndefinedKey:(NSString *)key

{

//取值方法

return
nil;

}

- (void)dealloc

{

[_name release];

[_sex release];

[_phone release];

[_number release];

[super dealloc];

}

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