經常使用第三方庫GDataXMLNode

IOS學習:經常使用第三方庫(GDataXMLNode:xml解析庫)


iOSXMLGDataXMLNodehtml

1、GDataXMLNode說明ios

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

使用方法:json

     一、獲取GDataXMLNode.h/m文件,將GDataXMLNode.h/m文件添加到工程中api

     二、向工程中增長「libxml2.dylib」庫微信

     三、在工程的「Build Settings」頁中找到「Header Search Path」項,添加/usr/include/libxml2"到路徑中app

     四、添加「GDataXMLNode.h」文件到頭文件中,如工程能編譯經過,則說明GDataXMLNode添加成功ide

2、GDataXMLNode示例學習

示例:ui

[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.     }  



    運行結果:

        spacer.gif

    


3、GDataXMLNode方法小結


     最終的數據讀出都是在GDataXMLElement對象中讀出的,如下方法均爲GDataXMLElement類的方法

     一、name方法,取標籤名 e.g name標籤的名稱「name」

     二、attributeForName: 取屬性結點 再調stringValue便可取到屬性值 e.g name標籤中的value屬性

     三、stringValue: 取標籤間的字符串值  e.g: age間的24

  • 1

相關文章
相關標籤/搜索