iOS开发学习之路【高级主题】——XML文件解析、JSON数据解析

XML文件解析

NSXMLParser

//初始化
-(instancetype)init{
    self = [super init];
    if (self) {
        NSBundle *b = [NSBundle mainBundle];
        NSString *path = [b pathForResource:@"customers" ofType:@".xml"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        self.parser = [[NSXMLParser alloc]initWithData:data];
        self.parser.delegate = self;
        
        self.list = [NSMutableArray arrayWithCapacity:5];
    }
    return self;
}
//开始元素
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{
    NSLog(@"didStartElement... %@",elementName);
    self.currentElement = elementName;
    if([self.currentElement isEqualToString:@"customer"]){
        self.customer = [[Customer alloc]init];
    }
    
}
//找到内容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"foundCharacters... %@",string);
    if([self.currentElement isEqualToString:@"id"]){
        int cid = [string intValue];
        [self.customer setCid:cid];
    }else if([self.currentElement isEqualToString:@"name"]){
        [self.customer setName:string];
    }else if([self.currentElement isEqualToString:@"name"]){
        int age = [string intValue];
        [self.customer setAge:age];
    }
}
//结束元素
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
    NSLog(@"didEndElement... %@",elementName);
    if([elementName isEqualToString:@"customer"]){
        [self.list addObject:self.customer];
    }
    self.currentElement = nil;
}

//开始文档
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"parserDidStartDocument...");
}
//结束文档
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"parserDidEndDocument...");
}

在这里插入图片描述

GDataXML

// 获取工程目录的xml文件
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"customers" ofType:@"xml"];
    NSData *xmlData = [[NSData alloc]initWithContentsOfFile:filePath];
    
    // 使用NSData对象初始化
    GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
    
    // 获取根节点(Users)
    GDataXMLElement *rootElement = [doc rootElement];
    
    // 获取根节点下的节点(User)
    NSArray *users = [rootElement elementsForName:@"customer"];
    
    for (GDataXMLElement *user in users) {
        // 获取User节点的值
        GDataXMLElement *idElement = [[user elementsForName:@"id"] objectAtIndex:0];
        NSString *cid = [idElement stringValue];
        NSLog(@"Customer id is:%@",cid);
        
        // 获取name节点的值
        GDataXMLElement *nameElement = [[user elementsForName:@"name"] objectAtIndex:0];
        NSString *name = [nameElement stringValue];
        NSLog(@"Customer name is:%@",name);
        
        // 获取age节点的值
        GDataXMLElement *ageElement = [[user elementsForName:@"age"] objectAtIndex:0];
        NSString *age = [ageElement stringValue];
        NSLog(@"Customer age is:%@",age);
        NSLog(@"----------------------");
    }

在这里插入图片描述

JSON数据解析

NSJSONSerialization

{
    "name":"中国",
    "province":[
        {
            "name":"黑龙江",
            "cities":{
                "city":["哈尔滨","大庆"]
            }
        },
        {
            "name":"浙江",
            "cities":{
                "city":["杭州","宁波"]
            }
        },
        {
            "name":"江苏",
            "cities":{
                "city":["南京","苏州"]
            }
        }
    ]
}

	NSBundle *b = [NSBundle mainBundle];
    NSString *path = [b pathForResource:@"json" ofType:@".json"];
    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"content=%@",content);
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[content dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"dic=%@",dic);
    
    NSString *name = [dic objectForKey:@"name"];
    NSLog(@"name=%@",name);
    NSArray *array = [dic objectForKey:@"province"];
    for(NSDictionary *dic in array){
        NSLog(@"dic=%@", dic);
    }

在这里插入图片描述

SBJSON

    NSBundle *b = [NSBundle mainBundle];
    NSString *path = [b pathForResource:@"json" ofType:@".json"];
    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"content=%@",content);
    
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    
    NSDictionary *dic = [parser objectWithString:content];
    NSLog(@"dic=%@",dic);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值