iOS GData解析xml

用GDaga解析xml

1.先到这里下载GData库,并导入

2.导入系统库libxml2.dylib

3.设置Build Settings,搜索header  ,设置header search paths为/usr/include/libxml2

4.在ViewController中 import GDataXMLNode.h文件

5.上代码,解析苹果appstore的游戏app排名

    //url
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/rss/topgrossingipadapplications/limit=30/xml"];
    //获取网页内容
    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    //获取xml文档
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:content options:0 error:nil];
至此已经获取到网页的xml文档,接下来就可以解析doc了

    //根节点
    GDataXMLElement *rootEle = [doc rootElement];
    NSLog(@"%@", rootEle);
    
    //获取根节点的子节点
    NSArray *rootChildren = [rootEle children];
    
    for (GDataXMLElement *rootChild in rootChildren) {
        NSLog(@"%@", rootChild.name);
    }

可以用rootChild.count查看子节点个数,或者可以获取某一个子节点:

    //获取第三个子节点
    GDataXMLElement *child = (GDataXMLElement *)[rootChildren objectAtIndex:2];
    //查看子节点的xmlString
    NSLog(@"%@", child.XMLString);

6.利用以上代码就可以解析xml了,但是这样会很别扭,因为我们必须得知道需要的节点是第几个,才能准确的获得我们想要的内容,所以在获取到doc以后可以用以下方法使得解析更加简单准确

    //初始化一个命名空间的字典
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"http://itunes.apple.com/rss", @"im", @"http://www.w3.org/2005/Atom", @"xmlns", nil];
    //根据命名空间解析doc,获取一个数组,命名空间缺省的是xmlns
    NSArray *array = [doc nodesForXPath:@"/xmlns:feed/xmlns:entry/im:name" namespaces:dict error:nil];
    //NSArray *array = [doc nodesForXPath:@"//im:name" namespaces:dict error:nil];//相对路径
for (GDataXMLElement *ele in array) { NSLog(@"%@", ele.stringValue); }



其中命名空间在xml文件中找,比如xmlns:im中im是命名空间,对于xmlns="http://www.w3.org/2005/Atom"则默认是xmlns

<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">

7.如果xml文件没有命名空间的话,解析就会更加简单

NSArray *array = [doc nodesForXPath:@"/feed" error:nil];
或者
NSArray *array = [doc nodesForXPath:@"//feed" error:nil];
前者使用了绝对路径,后者则使用了相对路径,而且两者可以混用,比如

NSArray *array = [doc nodesForXPath:@"//feed/id" error:nil];
另外,还可以使用逻辑或运算,则两者都会取到

NSArray *array = [doc nodesForXPath:@"//xmlns:id | //im:name" error:nil];

    for (GDataXMLElement *ele in array) {
        NSLog(@"%@-->%@", ele.name, ele.stringValue);
    }

打印结果是

2013-12-29 04:03:33.454 GDataXml[2636:f803] id-->https://itunes.apple.com/cn/rss/topgrossingipadapplications/limit=30/xml
2013-12-29 04:03:33.456 GDataXml[2636:f803] id-->https://itunes.apple.com/cn/app/clash-of-clans/id529479190?mt=8&uo=2
2013-12-29 04:03:33.457 GDataXml[2636:f803] im:name-->Clash of Clans
2013-12-29 04:03:33.458 GDataXml[2636:f803] id-->https://itunes.apple.com/cn/app/wo-jiaomt-online/id560104652?mt=8&uo=2
2013-12-29 04:03:33.459 GDataXml[2636:f803] im:name-->我叫MT Online
2013-12-29 04:03:33.460 GDataXml[2636:f803] id-->https://itunes.apple.com/cn/app/shen-diao-xia-lu/id668317926?mt=8&uo=2
2013-12-29 04:03:33.461 GDataXml[2636:f803] im:name-->神雕侠侣
2013-12-29 04:03:33.462 GDataXml[2636:f803] id-->https://itunes.apple.com/cn/app/tian-tian-fei-che/id728200220?mt=8&uo=2
2013-12-29 04:03:33.463 GDataXml[2636:f803] im:name-->天天飞车
2013-12-29 04:03:33.464 GDataXml[2636:f803] id-->https://itunes.apple.com/cn/app/quan-min-ying-xiong/id693996527?mt=8&uo=2



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值