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