主要是对文件的三种解析方法,相关的文件已经压缩附件中




.h文件

#import <UIKit/UIKit.h>

#import "StudentModel.h"

#import "GDataXMLNode.h"

@interface RootViewController : UIViewController<NSXMLParserDelegate>

@property(nonatomic,retain)NSMutableArray *dataArray;

//存放临时变量

@property(nonatomic ,retain)NSString *tempStr;

@end

.m文件


#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *button1 =[UIButton buttonWithType:UIButtonTypeCustom];

    [button1 setTitle:@"JSON" forState:UIControlStateNormal];

    UIButton *button2 =[UIButton buttonWithType:UIButtonTypeCustom];

    [button2 setTitle:@"DOM" forState:UIControlStateNormal];

    

    UIButton *button3 =[UIButton buttonWithType:UIButtonTypeCustom];

    [button3 setTitle:@"SAX" forState:UIControlStateNormal];

    button1.frame =CGRectMake(10, 80, 50, 20);

    button2.frame =CGRectMake(30,110, 50, 20);

    button3.frame =CGRectMake( 80, 150, 50, 20);

    button1.layer.borderWidth =1;

    button1.layer.borderColor =[UIColor blackColor].CGColor;

    button2.layer.borderWidth =1;

    button2.layer.borderColor =[UIColor blackColor].CGColor;

    button3.layer.borderWidth =1;

    button3.layer.borderColor =[UIColor blackColor].CGColor;

    [self.view addSubview:button1];

    [self.view addSubview:button2];

    [self.view addSubview:button3];

    self.view.backgroundColor =[UIColor redColor];

    [button1 addTarget:self action:@selector(change1) forControlEvents:UIControlEventTouchUpInside];

    [button2 addTarget:self action:@selector(change2) forControlEvents:UIControlEventTouchUpInside];

    [button3 addTarget:self action:@selector(change3) forControlEvents:UIControlEventTouchUpInside];

}

//解析json

-(void)change1

{

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

    //根据路径将文件转换为nsJSON

    NSData *data =[NSData dataWithContentsOfFile:path];

    self.dataArray =[NSMutableArray array];

    //创建错误信息

    NSError *error =nil;

    //获取数组(里面装的字典)

    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

    //遍历数组

    for (NSDictionary *dic in array) {

        StudentModel *stu =[StudentModel getStudentModel];

        [stu setValuesForKeysWithDictionary:dic];

        [self.dataArray addObject:stu];

    }

    //结果打印查看

    StudentModel *stu1 =[self.dataArray  objectAtIndex:1];

    NSLog(@"%@",stu1.name);

   }

-(void)change2

{

    //dom解析

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

//    NSData *data =[NSData dataWithContentsOfFile:path];

//    self.dataArray =[NSMutableArray array];

//    //转载数据(数据全部加入内存,因此说dom解析在解析大数据时比较消耗内存)

//    //1.参数代表数据 2参数没用 3参数是错误信息

//    NSError *error =nil;

//    GDataXMLDocument *document =[[GDataXMLDocument alloc]initWithData:data options:0 error:&error];

//    //获取根节点

//    GDataXMLElement *rootElement =[document rootElement];

//    //获取所有子节点

//    NSArray *array =[rootElement elementsForName:@"student"];

//    

//    //遍历

//    for (GDataXMLElement *node in array) {

//        StudentModel *stu =[StudentModel getStudentModel];

//      stu.name  = [[[node elementsForName:@"name"]lastObject]stringValue];

//        stu.sex =[[[node elementsForName:@"sex"]lastObject]stringValue];

//        stu.number =[[[[node elementsForName:@"number"]lastObject]stringValue]integerValue];

//        stu.phone =[[[[node elementsForName:@"phone"]lastObject]stringValue]integerValue];

//        [self.dataArray addObject:stu];

//      

//    }

//    StudentModel *model =[self.dataArray objectAtIndex:1];

//    NSLog(@"%@",model.name);

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

    NSData *data =[NSData dataWithContentsOfFile:path];

    self.dataArray =[NSMutableArray array];

 

    NSError *error =nil;

    GDataXMLDocument *document =[[GDataXMLDocument alloc]initWithData:data options:0 error:&error];

    GDataXMLElement *rootElement =[document rootElement];

    NSArray *array =[rootElement elementsForName:@"student"];

    for (GDataXMLElement *node in array) {

        StudentModel *stu =[StudentModel getStudentModel];

        stu.name =[[[node elementsForName:@"name"]lastObject]stringValue];

        stu.sex =[[[node elementsForName:@"sex"]lastObject]stringValue];

        stu.number =[[[[node elementsForName:@"number"]lastObject]stringValue] integerValue];

        stu.phone = [[[[node

                       elementsForName:@"phone"]lastObject]stringValue]integerValue];

        [self.dataArray addObject:stu];

    }

    StudentModel *model =[self.dataArray objectAtIndex: 1];

    NSLog(@"%@",model.name);   

}

-(void)change3

{

    //用sax方式来解析XML文件

    //获取要解析的文件路径

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

    //把这个文件转化为nsdata

    NSData *data =[NSData dataWithContentsOfFile:path];

    //创建解析类

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

    //设置代理

    parser.delegate =self;

    //开始解析

    [parser parse];



}

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

    //发现节点内容

   //记录节点内容

     self.tempStr =string;

}

#pragma -mark SAX解析代理方法



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

{

    //头节点

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

        self.dataArray =[NSMutableArray array];

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

        StudentModel *stu =[StudentModel getStudentModel];

        [self.dataArray addObject:stu];

    }

}

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



    //发现尾节点

    //取出stdentmodel

    StudentModel *stu =[self.dataArray lastObject];

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

        stu.number =[self.tempStr integerValue];

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

        stu.name =self.tempStr;

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

        stu.sex =self.tempStr;

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

        stu.phone =[self.tempStr integerValue];

    }

}

//解析完成

-(void)parserDidEndDocument:(NSXMLParser *)parser{

    NSLog(@"解析完成");

    StudentModel *stu =[self.dataArray objectAtIndex:1];

    NSLog(@"%@",stu.name);

}

@end