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 文件
代码和详细解释如下:
#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 文件
相关文章推荐
- iOS 在UILabel显示不同的字体和颜色(转)
- [Bluetooth Core V4.2] VOL2, PartB, 7 Bitstream Processing
- Android之学习笔记 Contacts (一)ContentResolver query 参数详解
- EOE 高仿UI特效专辑网址
- 区分手势UITapGestureRecognizer
- C#中HttpWebRequest与HttpWebResponse的使用方法
- UIbutton在UIScrollview中highlighted延迟
- 安全学习之burp suite-Proxy代理后无法后续Forward的问题
- hdu5033 Building 凸包
- IOS第二天多线程-05NSOperationQueue 暂停,和恢复队列任务
- UICollectionView学习<2>之UICollectionViewCell的自定义
- UIKit类关系总览
- easy ui 菜单和按钮(Menu and Button)
- AI--N Queens Problem(Look-Back method)
- LED调光,PFM即pulse frequence modulation
- Java使用阻塞队列BlockingQueue实现线程同步
- 执行maven-build.cmd失败
- HDU 5033 Building
- 修改easyui日期控件只显示年月,并且只能选择年月
- 进程pid,ppid;进程uid,euid;用户uid,guid