如何将GData库引入自己的工程,我就不说了,这个网上一大堆。下面直接讲如何解析XML文件
以http://api.douban.com/book/subjects?q=ios这个从豆瓣上请求的图书为例,下面是请求的内容
<Feedxmlns="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/">
<opensearch:startIndex>
1
</opensearch:startIndex>
<opensearch:totalResults>
810
</opensearch:totalResults>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<entry>
...
</entry>
<opensearch:itemsPerPage>
10
</opensearch:itemsPerPage>
</Feed>
上面红色字体是一本书所包含的全部信息,每本书都含有相同的内容,下面解析红色字体的全部内容
假设你已经使用ASIHttpRequest或者NSURLConnection或者其他的下载类讲这个XML文件的数据全部下载下来,并保存到(NSMutableData*)data中。
下面创建GdataxMLDocument类
NSError *error; GdataxMLDocument *doc=[[GdataxMLDocument alloc] initWithData:data options:0 error:&error]; if (!doc) { return; }
1、
GdataxMLElement * rootElement = [ doc rootElement]; NSArray * arrays = [ rootElement elementsForName:@"entry"];
2、
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"http://www.w3.org/2005/Atom",@"xmlns",nil]; NSArray *arrays=[doc nodesForXPath:@"/xmlns:Feed/xmlns:entry" namespaces:dict error:&error];
获取entry节点下所有节点的信息
原文链接:https://www.f2er.com/xml/299163.htmlfor (GdataxMLElement *element in array) { //<id>http://api.douban.com/book/subject/24846574</id> NSString * id = [[[ element elementsForName:@"id"]lastObject]stringValue]; NSLog(@"\nid --->%@",id); //<title>iOS开发指南:从零基础到App Store上架</title> NSString * title = [[[element elementsForName:@"title"]lastObject]stringValue]; NSLog(@"\ntitle --->%@",title); //<category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#book"/> NSString * scheme = [[[[element elementsForName:@"category"]lastObject]attributeForName:@"scheme"]stringValue]; NSLog(@"\nscheme --->%@",scheme); NSString * term = [[[[element elementsForName:@"category"]lastObject]attributeForName:@"term"]stringValue]; NSLog(@"\nterm --->%@",term); /* <author> <name>关东升</name> </author> */ NSString * name = [[[[[element elementsForName:@"author"]lastObject]elementsForName:@"name"]lastObject]stringValue]; NSLog(@"\nname --->%@",name); /* <link href="http://api.douban.com/book/subject/24846574" rel="self"/> <link href="http://book.douban.com/subject/24846574/" rel="alternate"/> <link href="http://img5.douban.com/spic/s26817976.jpg" rel="image"/> <link href="http://m.douban.com/book/subject/24846574/" rel="mobile"/> 获取image的URL,其他的类推 */ NSArray * links = [element elementsForName:@"link"]; for (GdataxMLElement * https in links) { GdataxMLNode * node = [https attributeForName:@"rel"]; if ([[node stringValue] isEqualToString:@"image"]) { GdataxMLNode * imageNOde = [https attributeForName:@"href"]; NSString * imageUrl = [imageNOde stringValue]; NSLog(@"\nimageUrl ---> %@",imageUrl); } } /* <db:attribute name="isbn10">7115324441</db:attribute> <db:attribute name="isbn13">9787115324443</db:attribute> <db:attribute name="author">关东升</db:attribute> <db:attribute name="price">99.00</db:attribute> <db:attribute name="publisher">人民邮电出版社</db:attribute> <db:attribute name="pubdate">2013-7</db:attribute> 获取出版社,其他的类推 */ NSArray * attributeArray = [element elementsForName:@"db:attribute"]; for (GdataxMLElement * element in attributeArray) { if ([[[element attributeForName:@"name"]stringValue] isEqualToString:@"publisher"]) { NSString * publisherName = [element stringValue]; NSLog(@"\npublisherName --->%@",publisherName); } } }
控制台输出结果:
id --->http://api.douban.com/book/subject/21356354 2014-02-17 15:20:48.970 DoubanDemo2[1678:70b] title --->iOS编程 2014-02-17 15:20:48.971 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.971 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.971 DoubanDemo2[1678:70b] name --->Aaron Hillegass 2014-02-17 15:20:48.971 DoubanDemo2[1678:70b] imageUrl ---> http://img5.douban.com/spic/s25011757.jpg 2014-02-17 15:20:48.972 DoubanDemo2[1678:70b] publisherName --->华中科技大学出版社 2014-02-17 15:20:48.972 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/24846574 2014-02-17 15:20:48.972 DoubanDemo2[1678:70b] title --->iOS开发指南:从零基础到App Store上架 2014-02-17 15:20:48.972 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.972 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.973 DoubanDemo2[1678:70b] name --->关东升 2014-02-17 15:20:48.973 DoubanDemo2[1678:70b] imageUrl ---> http://img5.douban.com/spic/s26817976.jpg 2014-02-17 15:20:48.973 DoubanDemo2[1678:70b] publisherName --->人民邮电出版社 2014-02-17 15:20:48.973 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/6392055 2014-02-17 15:20:48.974 DoubanDemo2[1678:70b] title --->iOS软件开发揭密 2014-02-17 15:20:48.974 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.974 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.975 DoubanDemo2[1678:70b] name --->虞斌 2014-02-17 15:20:48.975 DoubanDemo2[1678:70b] imageUrl ---> http://img5.douban.com/spic/s6477176.jpg 2014-02-17 15:20:48.975 DoubanDemo2[1678:70b] publisherName --->电子工业出版社 2014-02-17 15:20:48.976 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/25782512 2014-02-17 15:20:48.976 DoubanDemo2[1678:70b] title --->IOS 7:iPhone/iPad应用开发技术详解 2014-02-17 15:20:48.976 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.976 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.977 DoubanDemo2[1678:70b] name --->刘一道 2014-02-17 15:20:48.977 DoubanDemo2[1678:70b] imageUrl ---> http://img3.douban.com/spic/s27163550.jpg 2014-02-17 15:20:48.977 DoubanDemo2[1678:70b] publisherName --->机械工业出版社 2014-02-17 15:20:48.977 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/11610941 2014-02-17 15:20:48.978 DoubanDemo2[1678:70b] title --->iOS 5基础教程 2014-02-17 15:20:48.978 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.978 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.978 DoubanDemo2[1678:70b] name --->[美] Jeff LaMarche 2014-02-17 15:20:48.978 DoubanDemo2[1678:70b] imageUrl ---> http://img3.douban.com/spic/s11190761.jpg 2014-02-17 15:20:48.979 DoubanDemo2[1678:70b] publisherName --->人民邮电出版社 2014-02-17 15:20:48.979 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/19962787 2014-02-17 15:20:48.979 DoubanDemo2[1678:70b] title --->Objective-C编程 2014-02-17 15:20:48.980 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.980 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.980 DoubanDemo2[1678:70b] name --->[美] Aaron Hillegass 2014-02-17 15:20:48.980 DoubanDemo2[1678:70b] imageUrl ---> http://img3.douban.com/spic/s22704633.jpg 2014-02-17 15:20:48.981 DoubanDemo2[1678:70b] publisherName --->华中科技大学出版社 2014-02-17 15:20:48.981 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/19967897 2014-02-17 15:20:48.981 DoubanDemo2[1678:70b] title --->Objective-C 程序设计 2014-02-17 15:20:48.982 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.982 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.982 DoubanDemo2[1678:70b] name --->斯蒂芬·G·科昌 2014-02-17 15:20:48.982 DoubanDemo2[1678:70b] imageUrl ---> http://img3.douban.com/spic/s22783480.jpg 2014-02-17 15:20:48.982 DoubanDemo2[1678:70b] publisherName --->电子工业出版社 2014-02-17 15:20:48.982 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/6920082 2014-02-17 15:20:48.983 DoubanDemo2[1678:70b] title --->Objective-C编程之道 2014-02-17 15:20:48.983 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.983 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.983 DoubanDemo2[1678:70b] name --->钟冠贤 2014-02-17 15:20:48.983 DoubanDemo2[1678:70b] imageUrl ---> http://img3.douban.com/spic/s8855033.jpg 2014-02-17 15:20:48.983 DoubanDemo2[1678:70b] publisherName --->人民邮电 2014-02-17 15:20:48.984 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/3948373 2014-02-17 15:20:48.984 DoubanDemo2[1678:70b] title --->Objective-C 2.0程序设计 2014-02-17 15:20:48.984 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.984 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.984 DoubanDemo2[1678:70b] name --->Stephen G. Kochan 2014-02-17 15:20:48.984 DoubanDemo2[1678:70b] imageUrl ---> http://img3.douban.com/spic/s3957362.jpg 2014-02-17 15:20:48.985 DoubanDemo2[1678:70b] publisherName --->机械工业出版社 2014-02-17 15:20:48.985 DoubanDemo2[1678:70b] id --->http://api.douban.com/book/subject/3604295 2014-02-17 15:20:48.985 DoubanDemo2[1678:70b] title --->iPhone开发基础教程 2014-02-17 15:20:48.985 DoubanDemo2[1678:70b] scheme --->http://www.douban.com/2007#kind 2014-02-17 15:20:48.985 DoubanDemo2[1678:70b] term --->http://www.douban.com/2007#book 2014-02-17 15:20:48.985 DoubanDemo2[1678:70b] name --->Jeff LaMarche 2014-02-17 15:20:48.986 DoubanDemo2[1678:70b] imageUrl ---> http://img5.douban.com/spic/s3664046.jpg 2014-02-17 15:20:48.986 DoubanDemo2[1678:70b] publisherName --->人民邮电出版社