GData解析XML檔案

征途LN發表於2014-02-17

如何將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>
<id>http://api.douban.com/book/subject/24846574</id>
<title>iOS開發指南:從零基礎到App Store上架</title>
<category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#book"/>
<author>
<name>關東昇</name>
</author>
<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"/>
<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>
<gd:rating average="0" max="10" min="0" numRaters="8"/>
</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 --->人民郵電出版社




相關文章