加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用GDataXML解析XML文档

发布时间:2020-12-16 09:34:46 所属栏目:百科 来源:网络整理
导读:在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高、使用上更方便,关于IOS平台各种解析XML库的优缺点分析,可以看下这篇文章: http://www.raywenderlich.com/553/how-to-cho

在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
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <Users>
  3. <Userid="001"]]>
  4. <name>Ryan</name>
  5. <age>24</age>
  6. </User>
  7. <Userid="002"]]>
  8. <name>Tang</name>
  9. <age>23</age>
  10. </User>
  11. </Users>

接下来就可以开始解析了,在需要解析的文件中引入头文件:#import "GDataXMLNode.h"

我是新建的一个Empty工程,所以直接在AppDelegate.m中使用,代码如下:

[cpp] view plaincopy
  1. -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  2. {
  3. self.window=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
  4. //Overridepointforcustomizationafterapplicationlaunch.
  5. self.window.backgroundColor=[UIColorwhiteColor];
  6. [self.windowmakeKeyAndVisible];
  7. //获取工程目录的xml文件
  8. NSString*filePath=[[NSBundlemainBundle]pathForResource:@"users"ofType:@"xml"];
  9. NSData*xmlData=[[NSDataalloc]initWithContentsOfFile:filePath];
  10. //使用NSData对象初始化
  11. GDataXMLDocument*doc=[[GDataXMLDocumentalloc]initWithData:xmlDataoptions:0error:nil];
  12. //获取根节点(Users)
  13. GDataXMLElement*rootElement=[docrootElement];
  14. //获取根节点下的节点(User)
  15. NSArray*users=[rootElementelementsForName:@"User"];
  16. for(GDataXMLElement*userinusers){
  17. //User节点的id属性
  18. NSString*userId=[[userattributeForName:@"id"]stringValue];
  19. NSLog(@"Useridis:%@",userId);
  20. //获取name节点的值
  21. GDataXMLElement*nameElement=[[userelementsForName:@"name"]objectAtIndex:0];
  22. NSString*name=[nameElementstringValue];
  23. NSLog(@"Usernameis:%@",name);
  24. //获取age节点的值
  25. GDataXMLElement*ageElement=[[userelementsForName:@"age"]objectAtIndex:0];
  26. NSString*age=[ageElementstringValue];
  27. NSLog(@"Userageis:%@",age);
  28. NSLog(@"-------------------");
  29. }
  30. returnYES;
  31. }

编译执行在控制台输出结果如下:



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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读