iphone使用GDataXMLParser解析xml文件小結

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. }  
發佈了26 篇原創文章 · 獲贊 24 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章