GDataXMLNode解析xml文件的使用
来源:互联网 发布:鹏为软件股份有限公司 编辑:程序博客网 时间:2024/04/29 11:22
在做项目中需要对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
0 0
- GDataXMLNode解析xml文件的使用
- GDataXMLNode解析xml文件
- XML数据解析(GDataXMLNode的使用)
- XML解析:第三方库GDataXMLNode的使用
- IOS开发中使用GDataXmlNode解析xml
- iOS的XML解析库:GDataXMLNode
- GDataXMLNode解析xml数据的简单应用
- GDataXMLNode库 XML解析
- GDataXMLNode:xml解析库
- 怎样在XML解析时使用 GDataXmlNode方式,能够自动释放的方法
- XML解析之DOM解析引入GDataXMLNode的方法
- 网络:XML 解析(使用GDataXMLNode第三方框架)
- ios使用NSXMLParser类和 GDataXMLNode解析XML
- 采用GDataXMLNode进行xml解析
- iphone GDataXMLNode的使用
- 数据解析(二)解析XML之GDataXMLNode
- DOM解析(GDataXMLNode)详解,xml解析
- iOS 用GDataXMLNode创建和解析XML
- Maven使用总结
- git clone出错git upload-pack: git-pack-objects died with error
- Flex布局应用(1)——为什么flex只能做移动端APP开发?
- AES加密算法
- Spydroid的高级功能
- GDataXMLNode解析xml文件的使用
- 数据库表分区真能加速数据库查询速度吗?
- 手机app中跳转调用外部浏览器
- App Icons on iPad and iPhone
- Normal track, Fast track, Deep buffer, Codec offload 的区别
- backbone入门指南
- [hackerrank]Bob and Ben
- photos
- 判断按钮的点击和抬起