GData解析XML檔案
如何將GData庫引入自己的工程,我就不說了,這個網上一大堆。下面直接講如何解析XML檔案
以http://api.douban.com/book/subjects?q=ios這個從豆瓣上請求的圖書為例,下面是請求的內容
<feed 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/">
<title>搜尋 ios 的結果</title>
<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;
}
下面獲取所有的entry節點,這個有兩種方法:
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節點下所有節點的資訊
for (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 --->人民郵電出版社
相關文章
- jdom解析xml檔案XML
- jquery 解析xml檔案jQueryXML
- 使用 Java 解析XML檔案JavaXML
- 利用Perl解析XML檔案XML
- C#解析XML檔案C#XML
- python XML 檔案解析PythonXML
- XML 檔案解析實踐 (DOM 解析)XML
- 讀取xml檔案 解析雙層xmlXML
- Android之XML檔案解析AndroidXML
- Python解析XML檔案生成HTMLPythonXMLHTML
- Java XML檔案解析書目錄JavaXML
- python 解析xml 檔案: SAX方式PythonXML
- python 解析xml 檔案: DOM 方式PythonXML
- Python解析xml大檔案(sax)PythonXML
- AndroidManifest.xml檔案解析AndroidXML
- 使用PHP DOM-XML建立和解析XML檔案 (轉)PHPXML
- 如何使用 ABAP 程式碼解析 XML 檔案XML
- c#(解析xml檔案基礎方法)C#XML
- 基於 DOM 的 XML 檔案解析類XML
- jQuery解析xml檔案程式碼例項jQueryXML
- js載入解析xml檔案程式碼JSXML
- jQuery對xml檔案的解析例項jQueryXML
- python 解析xml 檔案: Element Tree 方式PythonXML
- mybatis原始碼配置檔案解析之五:解析mappers標籤(解析XML對映檔案)MyBatis原始碼APPXML
- springMVC---配置檔案解析(web.xml)SpringMVCWebXML
- Java DOM4J 方式解析XML檔案JavaXML
- PHP-四種解析XML檔案的方法PHPXML
- 關於SAX解析xml檔案的問題XML
- android下解析.plist配置檔案的xml解析器AndroidXML
- xml檔案XML
- js解析xml檔案簡單程式碼例項JSXML
- jquery實現的解析xml檔案程式碼例項jQueryXML
- js獲取並解析xml檔案程式碼例項JSXML
- struts2中struts.xml和web.xml檔案解析及工作原理XMLWeb
- mybatis的全域性配置檔案SqlMapConfig.xml解析MyBatisSQLXML
- ie中jQuery無法解析xml檔案的解決方案jQueryXML
- 用dom4j SAXReader解析xml檔案及字串XML字串
- 請問在解析xml檔案時報錯是什麼原因?XML