在做项目中需要对xml文件进行操作,最开始使用的是IOS自带的NSXMLParser,但是在使用过程中发现需要用到代理毁掉,使用起来比较麻烦。后面使用了第三方框架GDataXMLNode操作xml,感觉简单很多。下面介绍GDataXMLNode操作xml文件:
一、使用GDataXMLNode的项目设置:
1、将GDataXMLNode文件夹加入到工程
2、向Frameworks文件中添加libxml2.dylib库
3、Search Paths中 找到Header Search Paths 将其对应的值修改为:${SDK_DIR}/usr/include/libxml2
4、Linking中找到 Other Linker Flags 对应的值改为:-lxml2(这步我没设置也行)
二、使用GDataXMLNode:
1、读取文件并解析成GDataXMLDocument(相当于将xml文件内容解析成GDataXMLDocument对象):
NSData *xmlData=[NSData dataWithContentsOfFile:_xmlResourcePath];//从xml文件路径中获取xml的data数据
NSError *error = nil;//应为解析过程中可能存在error,所以需要捕获
GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
if(error != nil) {//如果存在错误则返回空,终止解析
return nil;
}
2、获取根元素并获取根元素的属性(解析一般从根元素解析):
GDataXMLElement *rootElement = xmlDocument.rootElement;//获取根元素
GDataXMLNode *rootCountNode=[rootElement attributeForName:@"count"];//获取根元素的某个属性,这里是获取count属性
NSString *count=[rootCountNode stringValue];//获取属性的值,NSString格式的
3、获取根元素下面的子元素(子元素以数组方式存在的):
NSArray *pointsArray = [rootElement elementsForName:@"Point"];//获取根元素下面的所有的Point子元素,以数组形式返回
if(pointsArray.count>0){
for(GDataXMLElement *pointElement in pointsArray){//解析子元素中的属性
mapPoint=[[MapPoint alloc] init];
mapPoint.bles=[[pointElement attributeForName:@"bles"] stringValue];
mapPoint.connIds=[[pointElement attributeForName:@"connIds"] stringValue];
mapPoint.coord=[[pointElement attributeForName:@"coord"] stringValue];
mapPoint.pointId=[[pointElement attributeForName:@"id"] stringValue];
}
}
4、在根元素下面添加子元素(添加操作):
GDataXMLElement *element = [GDataXMLNode elementWithName:@"Point"];//创建新的子元素
GDataXMLElement *elementAttributeId = [GDataXMLNode attributeWithName:@"id" stringValue:@"2323"];//为子元素设置属性
NSString *locationValue=[NSString stringWithFormat:@"%lf,%lf",locateion.coordinate.longitude,locateion.coordinate.latitude];
GDataXMLElement *elementAttributeLocation = [GDataXMLNode attributeWithName:@"location" stringValue:locationValue];
NSString *accuracyValue=[NSString stringWithFormat:@"%lf",locateion.horizontalAccuracy];
GDataXMLElement *elementAttributeAccuracy = [GDataXMLNode attributeWithName:@"accuracy" stringValue:accuracyValue];
GDataXMLElement *elementAttributeIndex = [GDataXMLNode attributeWithName:@"index" stringValue:elementIndexStr];
[element addAttribute:elementAttributeId];//子元素添加属性
[element addAttribute:elementAttributeLocation];
[element addAttribute:elementAttributeAccuracy];
[element addAttribute:elementAttributeIndex];
[rootElement addChild:element];//将子元素添加到根元素
5、保存修改后的xml元素(对xml文件进行增、删、改操作后保存修改后的xml文件,如果不保存则修改后文件内容不能反映到xml文件中):
// 生成xml文件内容
GDataXMLDocument *xmlDoc = [[GDataXMLDocument alloc] initWithRootElement:rootElement];
NSData *dataXml = [xmlDoc XMLData];
NSString *xmlFilePath=[self getLocatesXmlPath];//xml需要保存的路径
[dataXml writeToFile:xmlFilePath atomically:YES];
以上操作所用到的xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<Points count="57">
<Point id="506" location="114.065207,30.335351" accuracy="5.000000" index="2" />
<Point id="382" location="114.065154,30.335586" accuracy="5.000000" index="3" />
<Point id="269" location="114.065032,30.335876" accuracy="5.000000" index="4" />
<Point id="142" location="114.064836,30.336111" accuracy="5.833333" index="5" />
<Point id="72" location="114.064663,30.336249" accuracy="8.333333" index="6" />
<Point id="49" location="114.064394,30.336251" accuracy="5.322581" index="7" />
<Point id="37" location="114.064184,30.336345" accuracy="5.000000" index="8" />
<Point id="25" location="114.063898,30.336427" accuracy="5.000000" index="9" />
</Points>
GDataXMLNode下载地址:
http://download.csdn.net/detail/guobing19871024/9683669