在解析xml的时候,主要存在两种方式:SAX模式和DOM模式
SAX | DOM |
依序读入文件并产生相对应事件,可以处理任何大小的XML文件。 | 在内存中建立文件树,不适于处理大型的XML文件。 |
只能对文件按顺序剖析一遍,不支持对文件的随意存取 | 可以随意存取文件树的任何部分,没有次数限制 |
只能读取XML文件内容,而不能修改 | 可以随意修改文件树,从而修改了XML文件 |
开发上比较复杂,需要自己来制作事件处理器 | 易于理解,易于开发 |
对工作人员更灵活,可以用SAX建立自己的XML对象模型 | 已经在DOM基础之上建立了文件树 |
#import "ViewController.h" #import "studentModel.h" #import "GdataxMLNode.h" #import "JSONKit.h" @interface ViewController ()<NSXMLParserDelegate>//遵守xml的协议 { NSString *_currentElementName; } @property(nonatomic,strong)NSMutableArray *xml_SAXArray;///////////// @property(nonatomic,strong)NSMutableArray *xml_DOMArray;///////////// @property(nonatomic,strong)NSMutableArray *json_SystemArray;///////////// @property(nonatomic,strong)NSMutableArray *json_KitArray;///////////// @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark dom方法 - (IBAction)XML_DOMButtonDidClicked:(UIButton *)sender { self.xml_DOMArray = [NSMutableArray array];////////////////////////////////// //1 找到文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"xml"]; //2 读取文件路径 NSData *data = [NSData dataWithContentsOfFile:path]; //3 根据nsdata对象创建一个 GdataxMLDocument *document = [[GdataxMLDocument alloc]initWithData:data options:0 error:Nil]; //4 取出根节点 GdataxMLElement *rootElement = [document rootElement]; //5 拿出所有的student节点 NSArray *stuElementArray = [rootElement elementsForName:@"student"]; //6 遍历数组 拿到下一层节点 for (GdataxMLElement *element in stuElementArray) { //拿到name节点 GdataxMLElement *nameElement = [[element elementsForName:@"name"] lastObject]; //拿到name节点的值 NSString *name = [nameElement stringValue]; //拿到gender节点 GdataxMLElement *genderElement = [[element elementsForName:@"gender"] lastObject]; //拿到gender节点的值 NSString *gender = [genderElement stringValue]; //拿到age节点 GdataxMLElement *ageElement = [[element elementsForName:@"age"] lastObject]; //拿到age节点的值 NSString *age = [ageElement stringValue]; studentModel *model = [[studentModel alloc]init]; model.name = name; model.gender = gender; model.age = age; [self.xml_DOMArray addObject:model]; } for (studentModel *model in self.xml_DOMArray) { NSLog(@"%@ %@ %@",model.name,model.gender,model.age); } } //系统自带 #pragma mark sax方法 - (IBAction)xmlButtonDidClick:(UIButton *)sender { //1 找到文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"xml"]; //2 读取文件路径 NSData *data = [NSData dataWithContentsOfFile:path]; //3 解析 系统提供的方法 根据文件里的数据(data)初始化一个对象 NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data]; //4 设置xmlParser相关属性 //是否处理命名空间 [xmlParser setShouldProcessNamespaces:NO]; //是否报告命名空间 [xmlParser setShouldReportNamespacePrefixes:NO]; //5 设置代理 xmlParser.delegate = self; //6 开去解析 [xmlParser parse]; NSLog(@"qwerty"); } #pragma mark 开始解析的方法 打开文档 -(void)parserDidStartDocument:(NSXMLParser *)parser { self.xml_SAXArray = [NSMutableArray array]; } #pragma mark 打开标签的方法 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"student"]) { studentModel *model = [[studentModel alloc]init]; [self.xml_SAXArray addObject:model]; } _currentElementName = elementName; } #pragma mark 开始取值 -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { studentModel *stu = [self.xml_SAXArray lastObject]; if ([_currentElementName isEqualToString:@"name"]) { stu.name = string; } else if ([_currentElementName isEqualToString:@"gender"]) { stu.gender = string; } else if ([_currentElementName isEqualToString:@"age"]) { stu.age = string; } } #pragma mark 关闭标签 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { _currentElementName = Nil; } #pragma mark 关闭文档 解析结束 -(void)parserDidEndDocument:(NSXMLParser *)parser { for (studentModel *model in self.xml_SAXArray) { NSLog(@"%@ %@ %@",model.age); } } #pragma mark 使用系统自带方法解析json数据 - (IBAction)jsoFoundation:(UIButton *)sender { self.json_SystemArray = [NSMutableArray array]; //1 找到文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"json"]; //2 读取文件路径 NSData *data = [NSData dataWithContentsOfFile:path]; id d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:Nil]; NSArray *array = d; //循环数组 把字典转化为模型 for (NSDictionary * dict in array) { studentModel *model = [[studentModel alloc]init]; [model setValuesForKeysWithDictionary:dict]; [self.json_SystemArray addObject:model]; } for (studentModel *model in self.json_SystemArray) { NSLog(@"%@ %@ %@",model.number,model.hobby); } } #pragma mark 使用JSONKit解析json数据 - (IBAction)JSONKit:(UIButton *)sender { self.json_KitArray = [NSMutableArray array]; //1 找到文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"json"]; //2 读取文件路径 NSData *data = [NSData dataWithContentsOfFile:path]; //3 取出json数据 NSArray *array = [data objectFromJSONData]; // NSArray *arrar1 = @[@"adda",@"adad"]; for (NSDictionary *dict in array) { studentModel *model = [[studentModel alloc]init]; [model setValuesForKeysWithDictionary:dict]; [self.json_KitArray addObject:model]; } //验证 for (studentModel *model in self.json_KitArray) { NSLog(@"%@ %@ %@",model.hobby); } } @end