iOS xml 解析方法有两种:SAX , DOM.
SAX 不用借助第三方,在解析过程中逐行解析.(NSXMLParser,需要实现代理),具体步骤:
1.获取文件路径.
2.创建 NSXMLParser 对象.
3.遵守协议,设置代理.
4.实现代理方法
1>- (void)parserDidStartDocument: ---- 开始解析
2>- (void)parserDidEndDocument: ----解析结束
3>- (void)parser:didStartElement:namespaceURI:qualifiedName:attributes: ----遇到开始标签
4>- (void)parser:didEndElement:namespaceURI:qualifiedName: ----遇到结束标签
5>- (void)parser: foundCharacters: -----遇到值
详细代码:
1 - (IBAction)xmlParserWithSAX:(id)sender { 2 3 //获取文件路径 4 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"]; 5 //转换为url 6 NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath]; 7 //创建 NSXMLParser 对象 8 NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url]; 9 parser.delegate = self;10 //开始解析11 [parser parse];12 }
代理方法实现:
1 /** 2 * 开始解析 xml 3 */ 4 - (void)parserDidStartDocument:(NSXMLParser *)parser{ 5 NSLog(@"开始解析"); 6 self.studentArray = [[NSMutableArray alloc]init]; 7 } 8 9 10 /**11 * 结束解析 xml12 */13 - (void)parserDidEndDocument:(NSXMLParser *)parser{14 NSLog(@"解析结束");15 for (StudentModel *stu in self.studentArray) {16 NSLog(@"%@",stu);17 }18 }19 20 /**21 * 遇到开始标签22 */23 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{24 NSLog(@"遇到开始节点:%@",elementName);25 if ([elementName isEqualToString:@"student"]) {26 self.studentModel = [[StudentModel alloc]init];27 }28 }29 30 /**31 * 遇到结束标签32 */33 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{34 NSLog(@"遇到结束节点:%@",elementName);35 if ([elementName isEqualToString:@"name"]){36 self.studentModel.name = self.str;37 }else if ([elementName isEqualToString:@"age"]){38 self.studentModel.age = self.str;39 }else if ([elementName isEqualToString:@"gender"]){40 self.studentModel.gender = self.str;41 }else if ([elementName isEqualToString:@"student"]) {42 [self.studentArray addObject:self.studentModel];43 }44 }45 46 /**47 * 遇到的值48 */49 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{50 NSLog(@"%@",string);51 self.str = string;52 }
DOM 解析,先获取全部文件内容,找出层次结构,需要用到第三方(GDataXMLNode).具体步骤:
1.修改配置 Header Search Paths 加上:/usr/include/libxml2
Other Linker Flags 加上:-lxml2
2.获取文件路径
3.获取文件内容
4.获取 xml 结构
5.遍历节点取值
详细代码:
1 - (IBAction)xmlParserWithDOM:(id)sender { 2 3 //获取文件地址 4 NSString *filPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"]; 5 NSError *error = nil; 6 //获取文件内容 7 NSString *contact = [NSString stringWithContentsOfFile:filPath encoding:NSUTF8StringEncoding error:&error]; 8 if (error == nil) { 9 NSLog(@"%@",contact);10 }else {11 NSLog(@"%@",error);12 }13 //第三方14 GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:contact options:0 error:nil];15 16 //获取 xml 根节点17 GDataXMLElement *rootElement = [document rootElement];18 19 //获取 根节点包含的节点20 NSArray *array = [rootElement elementsForName:@"student"];21 22 NSMutableArray *arr = [[NSMutableArray alloc]init];23 24 //遍历节点,取值25 for (GDataXMLElement *ele in array) {26 StudentModel *model = [[StudentModel alloc]init];27 model.name = [[[ele elementsForName:@"name"] firstObject] stringValue];28 model.age = [[[ele elementsForName:@"age"] firstObject] stringValue];29 model.gender = [[[ele elementsForName:@"gender"] firstObject] stringValue];30 [arr addObject:model];31 32 }33 34 for (StudentModel *stu in arr) {35 NSLog(@"%@",stu);36 }37 38 }
新闻热点
疑难解答