用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];
for (GDataXMLElement *ele in array) { NSLog(@"%@", ele.stringValue); }//NSArray *array = [doc nodesForXPath:@"//im:name" namespaces:dict error:nil];//相对路径
其中命名空间在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