使用GDataXML解析XML文档
在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高、使用上更方便,关于IOS平台各种解析XML库的优缺点分析,可以看下这篇文章:http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project 这里主要介绍一下由Google提供的一种在IOS平台上进行XML解析的开源库GDataXML,可以到http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/下载源码,下载下来后进入文件夹找到XMLSupport文件夹,将里面的GDataXMLNode.h和GDataXMLNode.m文件拖拽到项目中新建的文件夹即可(我这里是建的GDataXML文件夹),注意要选中复制文件到项目中而不是只是引用,如图:
然后就是对工程进行一些配置,点击工程根目录然后点击左边的Target,进入Build Phases,然后点击第三个Link binary with libraries,点击加号搜索libxml2并将这个库添加到工程,如图:
接下来再进入Build Settings,在搜索框中搜索Head Search Path,然后双击并点击+按钮添加/usr/include/libxml2,如图:
接下来再搜索框中搜索Other linker flags,同样的方式添加-lxml2,如图:
到这里,添加和配置的工作就完成了(是有点麻烦),接下来就看如何使用了: 首先在工程中新建一个xml文件,作为我们要解析的对象,新建方法是在工程中新建一个Empty的文件,命名为users.xml,然后添加内容:
[html] view plaincopy
接下来就可以开始解析了,在需要解析的文件中引入头文件:#import "GDataXMLNode.h" 我是新建的一个Empty工程,所以直接在AppDelegate.m中使用,代码如下:
[cpp] view plaincopy
编译执行在控制台输出结果如下: GDataXml相同标签的多个属性GDataXml 相同标签的多个属性,好多文档都没有介绍获取属性的方法,让我找的好苦啊,必须分享!!!首先向Frameworks文件中添加libxml2.dylib这个库,而后再Croups & Files 侧边栏中双击我们的工程图标,找到 build 修改两个属性如下: 在Search Paths中 找到Header Search Paths将其对应的值修改为:/usr/includebxml2 在Linking中找到 Other Linker Flags 对应的值改为:-lxml2 GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithXMLString:resp*****eBody opti*****:2 error:nil]; if (doc!=nil) { GDataXMLElement *root=[doc rootElement ]; NSLog(@"--------root's children:--------n%@",root); //取出根节点的所有孩子节点 //取出某一个具体节点(body节点) [returnInfo setObject:[[[root elementsForName:@"db:uid"] objectAtIndex:0] stringValue] forKey:@"snsUserUid"]; [returnInfo setObject:[[[root elementsForName:@"title"]objectAtIndex:0]stringValue] forKey:@"snsNickName"]; [returnInfo setObject:[[[root elementsForName:@"db:location"]objectAtIndex:0]stringValue] forKey:@"snsProvince"]; [returnInfo setObject:[[[[root elementsForName:@"link"] objectAtIndex:2]attributeForName:@"href"] stringValue] forKey:@"snsProfileImageUrl"]; [returnInfo setObject:@"4" forKey:@"snsLandEntrance"]; NSLog(@"%@",[[[root elementsForName:@"link"] objectAtIndex:2]attributes]); NSLog(@"%@",[[[root elementsForName:@"db:location"]objectAtIndex:0]stringValue]); } NSLog(@"returnInforeturnInforeturnInforeturnInforeturnInfo%@",returnInfo); 附上xml源文件: <?xml version="1.0" encoding="UTF-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban.com/xmlns/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:opensearch="http://a9.com/-/spec/opensearchrss/1.0/"> <id>http://api.douban.com/people/63522291</id> <title>wangjianlewo</title> <link href="http://api.douban.com/people/63522291" rel="self"/> <link href="http://www.douban.com/people/63522291/" rel="alternate"/> <link href="http://img3.douban.com/icon/user_normal.jpg" rel="icon"/> <content></content> <db:attribute name="n_mails">0</db:attribute> <db:attribute name="n_notificati*****">0</db:attribute> <db:location id="beijing">北京</db:location> <db:signature></db:signature> <db:uid>63522291</db:uid> <uri>http://api.douban.com/people/63522291</uri> </entry> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Swift 3.1 发布,包含对标准库的改进
- Oracle 在windows下安装及plsql配置
- org.xml.sax.SAXParseException: The content of elements
- vb.net Linq四表联合查询
- FlexPaper控件实现文档的上传和预览
- ruby-on-rails – 如何在Rails 4 Pipeline中使用Galleria插
- 这是什么语法:在C#中的属性中使用“this”?
- ruby-on-rails – Rails新手如何找到Rails开发人员的工作?
- c – 链接对象和静态库
- React.js 小书 Lesson4 - 前端组件化(三):抽象出公共组件