数据解析

1.xml数据解析

 1>.SAX解析方式(Simple API for XML)

#pragma mark 实现NSXMLParser的代理方法
#pragma mark 开始解析(打开文档)
-(void)parserDidStartDocument:(NSXMLParser *)parser
{

    // 数组的初始化
    self.xmlSAXArray = [NSMutableArray array];
    
}
#pragma mark 打开标签
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

    if ([elementName isEqualToString:@"student"]) {
        Student *student = [[Student alloc] init];
        [self.xmlSAXArray addObject:student];
    }
    // 记录标签名字
    _currentElement = elementName;
}
#pragma mark 取值
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    
    // NSLog(@"%@",string);
    // 拿出数组中的学生对象
    Student *student = [self.xmlSAXArray lastObject];
    if ([_currentElement isEqualToString:@"name"]) {
        student.name = string;
    }else if ([_currentElement isEqualToString:@"sex"]){
    
        student.sex = string;
    }else if ([_currentElement isEqualToString:@"phone"]){
    
        student.phone = string;
    }
    
}
#pragma mark 关闭标签
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    _currentElement = nil;
    
}
#pragma mark 关闭文档
-(void)parserDidEndDocument:(NSXMLParser *)parser
{

    NSLog(@"*******************************SAX方式解析********************");
    
    // 打印验证
    for (Student *student in self.xmlSAXArray) {
        NSLog(@"%@ %@ %@",student.name,student.sex,student.phone);
    }
    
}

 2>DOM解析

 

2.JSON数据解析

 1>系统类

 2>三方类

 

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