XML、JSON解析

XML解析

#import "RootViewController.h"
#import "StudentModel.h"

@interface RootViewController ()

@property(nonatomic,strong)UIButton *saxbutton;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableString *myString;

@end

@implementation RootViewController

// 解析

// iOS常见的两种数据格式 : XML 和 JSON

// XML 解析方法 1. (系统方法)SAX解析  2. (google) GData方法

// JSON 1. 系统方法 2. JSONKit


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.saxbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.saxbutton.frame = CGRectMake(100, 100, 100, 100);
    self.saxbutton.backgroundColor = [UIColor blueColor];
    [self.saxbutton setTitle:@"sax解析" forState:UIControlStateNormal];
    self.saxbutton.tintColor = [UIColor redColor];
    [self.view addSubview:self.saxbutton];
   
    [self.saxbutton addTarget:self action:@selector(saxbuttonAction:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)saxbuttonAction:(UIButton *)sender{
    [self p_data];
}
// 处理数据的私有方法
- (void)p_data{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"txt"];
    // 先把数据从文件中读出来
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSLog(@"%@",data);
   
    // 1. 系统解析类
    NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data];
    // 2. 设置代理
    xmlParser.delegate = self;
    // 3. 开始解析
    [xmlParser parse];  
}
#pragma mark 代理方法
// 1. 开始解析文档
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"开始解析文件");
    self.dataArr = [NSMutableArray array];
}
// 2. 开始解析标签
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"student"]) {
        StudentModel *s = [[StudentModel alloc]init];
        // 判断标签名,如果是的话,就放到数组里面
        [self.dataArr addObject:s];
    }
}
// 3. 解析内容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    // 把内容拿到自己的字符串中
    self.myString = [NSMutableString string];
    [self.myString appendString:string];
}
// 4. 结束解析标签
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    StudentModel *s = [self.dataArr lastObject];
    [s setValue:self.myString forKey:elementName];
}
// 5. 结束解析文档
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    for (StudentModel *s in self.dataArr) {
        NSLog(@"name:%@ gender:%@ hobby:%@ word:%@",s.name,s.gender,s.hobby,s.word);
    }
}

XML_GData解析

#import "RootViewController.h"
#import "Person.h"
#import "GDataXMLNode.h"

@interface RootViewController ()

@property(nonatomic,strong)UIButton *domButton;
@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.domButton = [UIButton buttonWithType:UIButtonTypeSystem];
   
    self.domButton.frame = CGRectMake(100, 100, 100, 100);
   
    [self.domButton setTitle:@"dom解析" forState:UIControlStateNormal];
   
    [self.view addSubview:self.domButton];
   
    [self.domButton addTarget:self action:@selector(domButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    // Do any additional setup after loading the view.
}

- (void)domButtonAction:(UIButton *)sender{
    [self p_Gdate];
}
// 这里我们使用谷歌提供好的第三方,进行XML解析,这个第三方通过很好的封装,解析更稳定,更快
- (void)p_Gdate{
    NSLog(@"GDate解析");
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    self.dataArr = [NSMutableArray array];
   
    // 1. 初始化一个XMLDocument
    GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
   
    // 2. 生成根节点
    GDataXMLElement *rootElement = xmlDocument.rootElement;
    // 通过根标签 拿到子标签
    for (GDataXMLElement *childElement in rootElement.children) {
        Person *person = [[Person alloc]init];
        // stringvalue的属性可以拿到标签下面的值
        // name的属性,可以拿到标签的名字
        for (GDataXMLElement *valueElement in childElement.children) {
            [person setValue: valueElement.stringValue forKey:valueElement.name];
        }
        [self.dataArr addObject:person];
    }
    for (Person *per in self.dataArr) {
        NSLog(@"name:%@ gender:%@ hobby: %@ word: %@",per.name,per.gender,per.hobby,per.word);
    }
}

JSON解析

#import "RootViewController.h"
#import "Student.h"
#import "JSONKit.h"

@interface RootViewController ()

@property(nonatomic,strong)UIButton *jsonButton;

@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.jsonButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.jsonButton.frame = CGRectMake(100, 100, 100, 100);
    [self.jsonButton setTitle:@"JSON解析" forState:UIControlStateNormal];
    [self.view addSubview:self.jsonButton];
   
    [self.jsonButton addTarget:self action:@selector(jsonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    // Do any additional setup after loading the view.
}
- (void)jsonButtonAction:(UIButton *)sender{
    [self p_JSONdate];
}
- (void)p_JSONdate{
    // 使用系统方法
    /*
    NSLog(@"JSON解析");
    // 1. 获取文件路径
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data_json" ofType:@"txt"];
   
    // 2. 保存data
    NSData *data = [NSData dataWithContentsOfFile:filePath];
   
    // 3. 使用系统的方法进行json解析
   NSArray *tempArr =   [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    self.dataArr = [NSMutableArray array];
    // 4. forin 利用KVC进行赋值
    for (NSDictionary *d in tempArr) {
        Student *s = [[Student alloc]init];
        [s setValuesForKeysWithDictionary:d];
        [self.dataArr addObject:s];
    }
    for (Student *s in self.dataArr) {
        NSLog(@"name: %@ gender: %@ hobby: %@ word: %@",s.name,s.gender,s.hobby,s.word);
    }
  */
    //  使用第三方方法 JSONKit
    // 1. 获取文件路径
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"data_json" ofType:@"txt"];
    // 2. 保存data
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    self.dataArr = [NSMutableArray array];
    self.dataArr = [data objectFromJSONData];

    for (NSDictionary *s in self.dataArr) {
        Student *stu = [[Student alloc]init];
        [stu setValuesForKeysWithDictionary:s];
        NSLog(@"name:%@ gender: %@ hobby: %@ word: %@",stu.name,stu.gender,stu.hobby,stu.word);
    }
}

你可能感兴趣的:(XML、JSON解析)