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
<root>
<name value="wusj"/>
<age>24</age>
</root>
對此xml文件進行解析
[cpp] view plaincopy
NSString *xmlPath = [[NSBundlemainBundle] pathForResource:@"test"ofType:@"xml"];
NSString *xmlString = [NSStringstringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncodingerror:nil];
GDataXMLDocument *xmlDoc = [[GDataXMLDocumentalloc] initWithXMLString:xmlString options:0error:nil];
GDataXMLElement *xmlEle = [xmlDoc rootElement];
NSArray *array = [xmlEle children];
NSLog(@"count : %d", [array count]);
for (int i = 0; i < [array count]; i++) {
GDataXMLElement *ele = [array objectAtIndex:i];
// 根據標籤名判斷
if ([[ele name] isEqualToString:@"name"]) {
// 讀標籤裏面的屬性
NSLog(@"name --> %@", [[ele attributeForName:@"value"] stringValue]);
} else {
// 直接讀標籤間的String
NSLog(@"age --> %@", [ele stringValue]);
}
}
運行結果:
3、GDataXMLNode方法小結
最終的數據讀出都是在GDataXMLElement對象中讀出的,如下方法均爲GDataXMLElement類的方法
一、name方法,取標籤名 e.g name標籤的名稱「name」
二、attributeForName: 取屬性結點 再調stringValue便可取到屬性值 e.g name標籤中的value屬性
三、stringValue: 取標籤間的字符串值 e.g: age間的24
頂
1