@GDataXMLNode是Google提供的用于XML数据处理的类集。该类集对libxml2-DOM处理方式进行了封装,能对较小或中等的XML文档进行读写操作且支持XPath语法。
1.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”到其路径
5.搜索框中搜索Other linker flags,同样的方式添加-lxml2
@代码示例:
首先在工程中新建一个xml文件,作为我们要解析的对象,新建方法是在工程中新建一个Empty的文件,命名为Student.txt(可以命名为.xml,这个随意),然后添加内容:
<?xml version="1.0" encoding="UTF-8"?> <Students class="17班" school="中国"> <student> <number>1</number> <name>胡明涛</name> <sex>男</sex> <phone>123558</phone> </student> <student> <number>2</number> <name>成风采</name> <sex>男</sex> <phone>122323</phone> </student> <student> <number>3</number> <name>陈咬金</name> <sex>男</sex> <phone>21313558</phone> </student> <student sign="非应届生"> <number>4</number> <name>天天下</name> <sex>男</sex> <phone>56453558</phone> </student> </Students>
#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); } }
<?xml version="1.0"?> <xml_api_reply version="1"> <cities> <city> <name data="保定"/> <latitude_e6> 38849998</latitude_e6> <longitude_e6> 115569999</longitude_e6> </city> <city default="true" > <name data="北京"/> <latitude_e6> 39930000</latitude_e6> <longitude_e6> 116279998</longitude_e6> </city> <city> <name data="沈阳"/> <latitude_e6> 41770000</latitude_e6> <longitude_e6> 123430000</longitude_e6> </city> <city> <name data="成都"/> <latitude_e6> 30670000</latitude_e6> <longitude_e6> 104019996</longitude_e6> </city> <city> <name data="大连"/> <latitude_e6> 38900001</latitude_e6> <longitude_e6> 121629997</longitude_e6> </city> <city> <name data="福州"/> <latitude_e6> 26079999</latitude_e6> <longitude_e6> 119279998</longitude_e6> </city> <city> <name data="阜阳"/> <latitude_e6> 32930000</latitude_e6> <longitude_e6> 115830001</longitude_e6> </city> <city> <name data="赣州"/> <latitude_e6> 25850000</latitude_e6> <longitude_e6> 114949997</longitude_e6> </city> <city> <name data="广州"/> <latitude_e6> 23129999</latitude_e6> <longitude_e6> 113319999</longitude_e6> </city> <city> <name data="贵阳"/> <latitude_e6> 26579999</latitude_e6> <longitude_e6> 106720001</longitude_e6> </city> <city> <name data="哈尔滨"/> <latitude_e6> 45750000</latitude_e6> <longitude_e6> 126769996</longitude_e6> </city> <city> <name data="海口"/> <latitude_e6> 20030000</latitude_e6> <longitude_e6> 110349998</longitude_e6> </city> <city> <name data="邯郸"/> <latitude_e6> 38029998</latitude_e6> <longitude_e6> 114419998</longitude_e6> </city> <city> <name data="杭州"/> <latitude_e6> 30229999</latitude_e6> <longitude_e6> 120169998</longitude_e6> </city> <city> <name data="合肥"/> <latitude_e6> 31870000</latitude_e6> <longitude_e6> 117230003</longitude_e6> </city> <city> <name data="菏泽"/> <latitude_e6> 36119998</latitude_e6> <longitude_e6> 114370002</longitude_e6> </city> <city> <name data="衡阳"/> <latitude_e6> 36119998</latitude_e6> <longitude_e6> 114370002</longitude_e6> </city> </cities> </xml_api_reply>
#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); } } }