iphone使用GDataXMLParser解析xml檔案小結

來源:互聯網
上載者:User

GDataXMLParser是一個對xml具有良好操作的第三方庫。解析的方法如下: 
步驟: 
一、下載GDataXMLParser庫,把解壓後Source/XMLSupport/下的兩個檔案:GDataXMLNode.h和GDataXMLNode.m拖到工程中。 
一、加入libxml2.dylib架構。 
二、設定Search Paths中Header Search Paths為/usr/include/libxml2。 
三、在檔案中匯入標頭檔:GDataXMLNode.h。 
四、建立dom結構。 
Objective-c代碼  

  1. NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:@"test.xml"];  
  2. NSError *error;  
  3. GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];  


五、基於xpath的解析。 
Objective-c代碼  

  1. NSArray *themeAttr = [doc nodesForXPath:@"//theme" error:&error];  
  2. for(GDataXMLElement *themeElement in themeAttr){  
  3.     GDataXMLNode *themeIDNode = [themeElement attributeForName:@"id"];//解析屬性  
  4.     int themeID = [themeIDNode.stringValue intValue];//數字  
  5.       
  6.     //theme url  
  7.     GDataXMLNode *themeURLNode = [themeElement attributeForName:@"url"];//字串  
  8.     NSString *themeURL = themeURLNode.stringValue;  
  9. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.