1. 從http://code.google.com/p/gdata-objectivec-client/downloads/list下載“gdata-objective-c client library.”
2. 解壓縮文件,找到Source\XMLSupport,並且將其中的GDataXMLNode.h 和 GDataXMLNode.m文件拖到項目中
3. 選中項目,選中“Build Settings”標簽頁
4. 將Build Settings頁中,頂部的“Basic”標簽切換到“All”
5. 找到“Paths\Header Search Paths”項,並添加“/usr/include/libxml2”到列表中
6. 找到“Linking\Other Linker Flags”項,並添加“-lxml2”到列表中
在需要調用GDataXML的代碼文件頭部,加入:
#import "GDataXMLNode.h"
應用示例:創建DOM結構
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:@"test.xml"];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
基於XPath解析
NSArray *themeAttr = [doc nodesForXPath:@"//theme" error:&error];
for(GDataXMLElement *themeElement in themeAttr){
GDataXMLNode *themeIDNode = [themeElement attributeForName:@"id"];//解析屬性
int themeID = [themeIDNode.stringValue intValue];//數字
//theme url
GDataXMLNode *themeURLNode = [themeElement attributeForName:@"url"];//字符串
NSString *themeURL = themeURLNode.stringValue;
}