数据解析(二)解析XML之GDataXMLNode

最后更新于:2022-04-01 22:58:56

@GDataXMLNode是Google提供的用于XML数据处理的类集。该类集对libxml2-DOM处理方式进行了封装,能对较小或中等的XML文档进行读写操作且支持XPath语法。     1.[http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/](http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/),获得GDataXMLNode.h和GDataXMLNode.m文件,进行如下操作将文件导入到Xcode的工程中:     2.将GDataXMLNode.h/m文件添加到工程中;     3.向工程中增加“libxml2.dylib”库;     4.在工程的“Build Settings”页中找到“Header Search Path”项,添加“/usr/include/libxml2”到其路径 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-11_569358af7d570.jpg)     5.搜索框中搜索Other linker flags,同样的方式添加-lxml2 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-11_569358af99a1d.jpg) **@代码示例:**       首先在工程中新建一个xml文件,作为我们要解析的对象,新建方法是在工程中新建一个Empty的文件,命名为Student.txt(可以命名为.xml,这个随意),然后添加内容: ~~~ 1 胡明涛 123558 2 成风采 122323 3 陈咬金 21313558 4 天天下 56453558 ~~~ ~~~ #pragma mark - GDataXMLNode - (void)onClickGDataXMLButton{ // 获取工程目录的xml文件 NSData * xmlData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Student" ofType:@"txt"]]; // 从文档中读出完整的XML数据,在内存中形成完整的树形结构 NSError * error = nil; GDataXMLDocument * documents = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&error]; // 取得根节点(element元素)---Students GDataXMLElement * rootEL = [documents rootElement]; // 获取根节点下的节点,返回的数组---student NSArray * studentsArray = [rootEL elementsForName:@"student"]; for (int i = 0; i < [studentsArray count]; i++) { // 获取单个节点对象 GDataXMLElement * student = [studentsArray objectAtIndex:i]; // 获取student节点下name,number,sex,phone节点的值 GDataXMLElement * nameElement = [[student elementsForName:@"name"] lastObject]; GDataXMLElement * sexElement = [[student elementsForName:@"sex"]lastObject]; GDataXMLElement * numberElement = [[student elementsForName:@"number"]lastObject]; GDataXMLElement * phoneElement = [[student elementsForName:@"phone"]lastObject]; NSString * name = [nameElement stringValue]; NSString * number = [numberElement stringValue]; NSString * sex = [sexElement stringValue]; NSString * phone = [phoneElement stringValue]; // 读标签里面的属性(attributeForName) NSString * sign = [[student attributeForName:@"sign"]stringValue]; if (sign) { NSLog(@"----------%@",sign); } NSLog(@"%@,%@,%@,%@",number,name,sex,phone); } #pragma mark 通过路径,取得指定的节点值,所有节点中同一标签的值 // 一个是相对路径,一个是绝对路径-----------(nodesForXPath) //NSArray * nameArray = [rootEL nodesForXPath:@"//name" error:nil]; NSArray * nameArray = [rootEL nodesForXPath:@"student/name" error:nil]; for (int i = 0; i < [nameArray count]; i++) { NSString * name = [[nameArray objectAtIndex:i] stringValue]; NSLog(@"---%@",name); } } ~~~ ~~~ 38849998 115569999 39930000 116279998 41770000 123430000 30670000 104019996 38900001 121629997 26079999 119279998 32930000 115830001 25850000 114949997 23129999 113319999 26579999 106720001 45750000 126769996 20030000 110349998 38029998 114419998 30229999 120169998 31870000 117230003 36119998 114370002 36119998 114370002 ~~~ ~~~ #pragma mark - 解析citys - (void)parseCitys{ NSData * xmlCitysData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"citys" ofType:@"xml"]]; // 从文档中读取完成的XML文档,在内存中形成完整的树形结构 NSError * error = nil; GDataXMLDocument * documents = [[GDataXMLDocument alloc]initWithData:xmlCitysData options:0 error:&error]; // 取得根节点 GDataXMLElement * rootNode = [documents rootElement]; #pragma mark 获取根节点xml_api_reply的数组,包含的是cities NSArray * citiesArray = [rootNode elementsForName:@"cities"]; for (int i = 0; i < [citiesArray count]; i++) { // 取得单个cities节点 GDataXMLElement * cities = [citiesArray objectAtIndex:i]; #pragma mark 获取节点cities的数组,包含的是city NSArray * cityArray = [cities elementsForName:@"city"]; for (int j = 0; j < [cityArray count]; j++) { // 取得单个city节点 GDataXMLElement * city = [cityArray objectAtIndex:j]; #pragma mark city节点下面是3个并行的节点,都是1个,直接通过lastObject来取得想要的值 NSString * name = [[[[city elementsForName:@"name"]lastObject] attributeForName:@"data"] stringValue]; NSLog(@"name = %@",name); NSString * latitude_e6 = [[[city elementsForName:@"latitude_e6"]lastObject] stringValue]; NSLog(@"latitude_e6 = %@",latitude_e6); NSString * longitude_e6 = [[[city elementsForName:@"longitude_e6"]lastObject] stringValue]; NSLog(@"longitude_e6 = %@",longitude_e6); } } } ~~~ @GDataXMLNode方法小结:      最终的数据读出都是在GDataXMLElement对象中读出的,以下方法均为GDataXMLElement类的方法      1、elementsForName方法,取标签名  name标签的名称“name”      2、attributeForName: 取属性结点 再调stringValue即可取到属性值       3、stringValue: 取标签间的字符串值
';