IOS_UI_数据解析

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 文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值