iOS第三方類庫之-GDataXMLNode

來源:互聯網
上載者:User

標籤:blog   http   color   使用   strong   os   

一、GDataXMLNode說明 

GDataXMLNode是Google提供的用於XML資料處理的類集。該類集對libxml2--DOM處理方式進行了封裝,能對較小或中等的xml文檔進行讀寫操作且支援XPath文法。

 使用方法:     1、擷取GDataXMLNode.h/m檔案,將GDataXMLNode.h/m檔案添加到工程中     2、向工程中增加“libxml2.dylib”庫     3、在工程的“Build Settings”頁中找到“Header Search Path”項,添加/usr/include/libxml2"到路徑中     4、添加“GDataXMLNode.h”檔案到標頭檔中,如工程能編譯通過,則說明GDataXMLNode添加成功 二、GDataXMLNode樣本 樣本: [html] view plaincopy
  1. <root>  
  2.      <name value="wusj"/>  
  3.      <age>24</age>  
  4. </root>  
 對此xml檔案進行解析 

    

[cpp] view plaincopy
  1. NSString *xmlPath = [[NSBundlemainBundle] pathForResource:@"test"ofType:@"xml"];  
  2.     NSString *xmlString = [NSStringstringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncodingerror:nil];  
  3.     GDataXMLDocument *xmlDoc = [[GDataXMLDocumentalloc] initWithXMLString:xmlString options:0error:nil];  
  4.     GDataXMLElement *xmlEle = [xmlDoc rootElement];  
  5.     NSArray *array = [xmlEle children];  
  6.     NSLog(@"count : %d", [array count]);  
  7.      
  8.     for (int i = 0; i < [array count]; i++) {  
  9.         GDataXMLElement *ele = [array objectAtIndex:i];  
  10.           
  11.         // 根據標籤名判斷  
  12.         if ([[ele name] isEqualToString:@"name"]) {  
  13.             // 讀標籤裡面的屬性  
  14.             NSLog(@"name --> %@", [[ele attributeForName:@"value"] stringValue]);  
  15.         } else {  
  16.             // 直接讀標籤間的String  
  17.             NSLog(@"age --> %@", [ele stringValue]);  
  18.         }  
  19.          
  20.     }  

 

     運行結果:             三、GDataXMLNode方法小結      最終的資料讀出都是在GDataXMLElement對象中讀出的,以下方法均為GDataXMLElement類的方法     1、name方法,取標籤名 e.g name標籤的名稱“name”     2、attributeForName: 取屬性結點 再調stringValue即可取到屬性值 e.g name標籤中的value屬性     3、stringValue: 取標籤間的字串值  e.g: age間的24

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.