XML解析主要有两种方式:DOM第三方的解析和Sax官方的解析。
1.DOM解析
1)XML DOM解析包:GdataxMLNode 点击打开链接
2)导入静态库
// libxml includes require that the target Header Search Paths contain // // /usr/include/libxml2 // // and Other Linker Flags contain // // -lxml2
3)导入libxml2库
4)我们先看看 我们写的 xml文件
<?xml version ="1.0" encoding = "utf-8" ?>
<!--学生列表-->
<student_list>
<student>
<stu_num>001</stu_num>
<stu_name>李蝉</stu_name>
<stu_class>一班</stu_class>
</student>
<student>
<stu_num>002</stu_num>
<stu_name>李2</stu_name>
<stu_class>二班</stu_class>
</student>
</student_list>
5)解析的代码如下:
- (IBAction)domParser:(id)sender { //首先获取 我们的xml文件的路径,并获得相应的 xml文件的字符串 NSString *path = [[NSBundle mainBundle]pathForResource:@"student_list" ofType:@"xml"]; NSString *xmlString = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",xmlString); //传送给解析器
//把string封装成 GdataxMLDocument ,准备解析 GdataxMLDocument *document = [[GdataxMLDocument alloc]initWithXMLString:xmlString options:0 error:nil]; //依据我们的XML文件 获得根节点 GdataxMLElement *rootElement = [document rootElement]; //获得 root节点下 的 student 节点的所有。 NSArray *stuArr = [rootElement elementsForName:@"student"]; //对 student节点进行输出 for (GdataxMLNode *node in stuArr) { NSLog(@"-------------student-------"); for (int i = 0; i < [node childCount]; i++) { NSArray * infoArr = [node children]; GdataxMLNode *infoNode = [infoArr objectAtIndex:i]; switch (i) { case 0: { [self.textString appendFormat:@"stu_num%@\n",[infoNode stringValue] ]; NSLog(@"%@",[infoNode stringValue]); NSLog(@"%@",self.textString); break; } case 1: [self.textString appendFormat:@"stu_name%@ \n",[infoNode stringValue] ]; break; case 2: [self.textString appendFormat:@"stu_class%@ \n",[infoNode stringValue]]; break; default: break; } } } self.textView.text = self.textString; NSLog(@"view:%@",self.textString); }
2.SAX解析
sax解析主要是依赖代理的方法进行的。NSXMLParserDelegate
1)sax解析的前期准备,配置xml文件以及 设置delegate代理
- (IBAction)saxParser:(id)sender { NSString *path = [[NSBundle mainBundle]pathForResource:@"student_list" ofType:@"xml"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data]; parser.delegate = self; [parser parse]; }
2)NSXMLParserDelegate代理文件
- (void)parserDidStartDocument:(NSXMLParser *)parser {
//在 解析doument的开始调用。 NSLog(@"文档解析开始"); } - (void)parserDidEndDocument:(NSXMLParser *)parser {
//解析结束时调用 NSLog(@"文档解析结束"); } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//解析一个标签的时候调用,如果我们想得到的只是某个标签,只要设置对应的elementName ,并设置全局变量的真假 ,然后在parser中进行性的输出就行了。 NSLog(@"标签开始"); NSLog(@"didStartElement %@",elementName); if ([elementName isEqualToString:@"stu_name"]) { print = YES; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//解析一个标签结束时候调用 NSLog(@"标签结束"); NSLog(@"didEndElement %@",elementName); } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { //得到对应的标签的内部数据 if (print) { NSLog(@"Data:%@",string); print = YES; } }//不进行限制时候的情况:
2014-01-01 17:53:04.597 XML解析[8050:a0b] 文档解析开始
2014-01-01 17:53:04.597 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.597 XML解析[8050:a0b] didStartElement student_list
2014-01-01 17:53:04.598 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.598 XML解析[8050:a0b] didStartElement student
2014-01-01 17:53:04.598 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.598 XML解析[8050:a0b] didStartElement stu_num
2014-01-01 17:53:04.598 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.599 XML解析[8050:a0b] didEndElement stu_num
2014-01-01 17:53:04.599 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.599 XML解析[8050:a0b] didStartElement stu_name
2014-01-01 17:53:04.599 XML解析[8050:a0b] Data:李蝉
2014-01-01 17:53:04.599 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.600 XML解析[8050:a0b] didEndElement stu_name
2014-01-01 17:53:04.600 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.600 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.600 XML解析[8050:a0b] didStartElement stu_class
2014-01-01 17:53:04.600 XML解析[8050:a0b] Data:一班
2014-01-01 17:53:04.600 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.600 XML解析[8050:a0b] didEndElement stu_class
2014-01-01 17:53:04.601 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.601 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.601 XML解析[8050:a0b] didEndElement student
2014-01-01 17:53:04.601 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.601 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.601 XML解析[8050:a0b] didStartElement student
2014-01-01 17:53:04.602 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.602 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.602 XML解析[8050:a0b] didStartElement stu_num
2014-01-01 17:53:04.602 XML解析[8050:a0b] Data:002
2014-01-01 17:53:04.602 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.602 XML解析[8050:a0b] didEndElement stu_num
2014-01-01 17:53:04.602 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.603 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.603 XML解析[8050:a0b] didStartElement stu_name
2014-01-01 17:53:04.603 XML解析[8050:a0b] Data:李2
2014-01-01 17:53:04.603 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.603 XML解析[8050:a0b] didEndElement stu_name
2014-01-01 17:53:04.603 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.604 XML解析[8050:a0b] 标签开始
2014-01-01 17:53:04.604 XML解析[8050:a0b] didStartElement stu_class
2014-01-01 17:53:04.604 XML解析[8050:a0b] Data:二班
2014-01-01 17:53:04.604 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.604 XML解析[8050:a0b] didEndElement stu_class
2014-01-01 17:53:04.604 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.604 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.605 XML解析[8050:a0b] didEndElement student
2014-01-01 17:53:04.605 XML解析[8050:a0b] Data:
2014-01-01 17:53:04.605 XML解析[8050:a0b] 标签结束
2014-01-01 17:53:04.605 XML解析[8050:a0b] didEndElement student_list
2014-01-01 17:53:04.605 XML解析[8050:a0b] 文档解析结束
2014-01-01 17:54:06.230 XML解析[8074:a0b] 文档解析开始
2014-01-01 17:54:06.231 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.232 XML解析[8074:a0b] didStartElement student_list
2014-01-01 17:54:06.232 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.233 XML解析[8074:a0b] didStartElement student
2014-01-01 17:54:06.234 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.234 XML解析[8074:a0b] didStartElement stu_num
2014-01-01 17:54:06.235 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.235 XML解析[8074:a0b] didEndElement stu_num
2014-01-01 17:54:06.236 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.237 XML解析[8074:a0b] didStartElement stu_name
2014-01-01 17:54:06.237 XML解析[8074:a0b] Data:李蝉
2014-01-01 17:54:06.238 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.238 XML解析[8074:a0b] didEndElement stu_name
2014-01-01 17:54:06.239 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.239 XML解析[8074:a0b] didStartElement stu_class
2014-01-01 17:54:06.240 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.240 XML解析[8074:a0b] didEndElement stu_class
2014-01-01 17:54:06.241 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.241 XML解析[8074:a0b] didEndElement student
2014-01-01 17:54:06.241 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.242 XML解析[8074:a0b] didStartElement student
2014-01-01 17:54:06.242 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.243 XML解析[8074:a0b] didStartElement stu_num
2014-01-01 17:54:06.243 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.244 XML解析[8074:a0b] didEndElement stu_num
2014-01-01 17:54:06.244 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.245 XML解析[8074:a0b] didStartElement stu_name
2014-01-01 17:54:06.245 XML解析[8074:a0b] Data:李2
2014-01-01 17:54:06.246 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.246 XML解析[8074:a0b] didEndElement stu_name
2014-01-01 17:54:06.247 XML解析[8074:a0b] 标签开始
2014-01-01 17:54:06.247 XML解析[8074:a0b] didStartElement stu_class
2014-01-01 17:54:06.248 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.248 XML解析[8074:a0b] didEndElement stu_class
2014-01-01 17:54:06.249 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.249 XML解析[8074:a0b] didEndElement student
2014-01-01 17:54:06.250 XML解析[8074:a0b] 标签结束
2014-01-01 17:54:06.250 XML解析[8074:a0b] didEndElement student_list
2014-01-01 17:54:06.251 XML解析[8074:a0b] 文档解析结束
对比之后可以看出,虽然也调用了方法,但是因为 print为false,所以并没有打印输出。 原文链接:https://www.f2er.com/xml/299334.html