xml json 解析

//

//  xmlController.m

//  XML

//

//  Created by 0卜7r从l on 13-11-26.

//  Copyright (c) 2013年 0卜7r从l. All rights reserved.

//


#import "xmlController.h"


@interface xmlController ()


@end


@implementation xmlController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

  

    self.stuMuArray=[[NSMutableArray alloc]init];

    [_stuMuArray release];

    // Do any additional setup after loading the view from its nib.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc {

    [_stuMuArray release];

    [_xmlSystem release];

    [_Dom release];

    [_json release];

    [_jsonkit release];

    [super dealloc];

}




//jsonkit 解析

- (IBAction)jsonKit:(id)sender {

    //获取路径

    NSString* path=[[NSBundle mainBundle]pathForResource:@"student.json" ofType:Nil];

    

    NSData *data=[NSData dataWithContentsOfFile:path];

    

    NSArray* array=[data objectFromJSONData];

    [array JSONData];

}







- (IBAction)xmlSystemAction:(id)sender {

    //获取路径

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

    

    NSData *data=[NSData dataWithContentsOfFile:path];

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

    parser.delegate=self;

    [parser parse];

}

/**

 *  解析器找到开始标签

 *

 *  @param parser        解析器对象

 *  @param elementName   标签名字

 *  @param namespaceURI  命名空间

 *  @param qName         添了前缀的标签

 *  @param attributeDict 属性

 */

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

    NSLog(@"~|~~~%@",elementName);

}

//找到结束点

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

    NSLog(@"~?~~~%@",elementName);

}

//取值

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

    NSLog(@"~value~~~%@",string);

}

//dom解析

- (IBAction)DomAction:(id)sender {

    

    //获取路径

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

    

    NSData *data=[NSData dataWithContentsOfFile:path];

    GDataXMLDocument *xmlData=[[GDataXMLDocument alloc]initWithData:data options:0 error:Nil];

    GDataXMLElement *root=[xmlData rootElement];

    NSLog(@"~~~~~~%@",root);

    //找到某个节点下得所有某个子节点

    NSArray * stuArray=[root elementsForName:@"student"];

    NSLog(@"!!!!!%@",stuArray);

    for (GDataXMLElement *dic in stuArray) {

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

        //取属性

        GDataXMLNode *node=[dic attributeForName:@"type"];

        NSString* value1=[node stringValue];

        NSLog(@"~~~~~~~~~~~~~~%@",value1);

        NSArray *names=[dic elementsForName:@"name"];

        GDataXMLElement *name=[names objectAtIndex:0];

        NSArray *sexs=[dic elementsForName:@"sex"];

        GDataXMLElement *sex=[sexs objectAtIndex:0];

        NSArray *numbers=[dic elementsForName:@"number"];

        GDataXMLElement *number=[numbers objectAtIndex:0];

        stu.name=name.stringValue;

        stu.sex=sex.stringValue;

        stu.number=[number.stringValue intValue];

        [self.stuMuArray addObject:stu];

        [stu release];

    }

    

    //xpath

    NSArray *nameArray=[root nodesForXPath:@"//name" error:Nil];

    NSLog(@"***************======%@",nameArray);

    

    //打印stu数组

    for (Student *stu in _stuMuArray) {

        NSLog(@"数组::::%@",stu.name);

    }

    

}















//解析json

- (IBAction)jsonAction:(id)sender {

    //获取路径

    NSString* path=[[NSBundle mainBundle]pathForResource:@"student.json" ofType:Nil];

    

    NSData *data=[NSData dataWithContentsOfFile:path];

    NSArray *array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:Nil];

    //id 转json

    NSData *dataJson=[NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:Nil];

    NSLog(@"%@",array);

    

}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值