步驟:
一、下載GDataXMLParser庫,把解壓後Source/XMLSupport/下的兩個文件:GDataXMLNode.h和GDataXMLNode.m拖到工程中。
一、加入libxml2.dylib框架。
二、設置Search Paths中Header Search Paths爲/usr/include/libxml2。
三、在文件中導入頭文件: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;
- }