ios的google解析XML框架GDataXML的配置及使用

GDataXMLnode

1.下載連接:http://code.google.com/p/gdata-objectivec-client/downloads/list下載「gdata-objective-c client library.」web

2.項目引入: 解壓縮文件,找到Source\XMLSupport,而且將其中的GDataXMLNode.h 和 GDataXMLNode.m文件拖到項目中objective-c

3. 項目編譯支持配置:ui

1). 選中項目,選中「Build Settings」標籤頁google

2 ). 將Build Settings頁中,頂部的「Basic」標籤切換到「All」spa

3). 找到「Paths\Header Search Paths」項,並添加「/usr/include/libxml2」到列表中code

4). 找到「Linking\Other Linker Flags」項,並添加「-lxml2」到列表中orm

4。 在要用到的地方引入「#import "GDataXMLNode.h"」xml

而後就可使用了,下面的是一個本身項目 中使用的小例子ci

/**
 解析webservice返回的XML成一個NSDictionary
 參數:content ,要解析的數據
 參數:path   ,要解析的XML數據一個根節點
 返回:NSDictionary
 */
+ (NSDictionary *)getWebServiceXMLResult:(NSString *) content xpath:(NSString *)path
{
    NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] init];
    content =  [content stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    content =  [content stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];        
    content = [content stringByReplacingOccurrencesOfString:@"xmlns" withString:@"noNSxml"];
    NSError *docError = nil;
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:content options:0 error:&docError];
    if(!docError)
    {
        NSArray *children = nil;
        children = [document nodesForXPath:[NSString stringWithFormat:@"//%@",path] error:&docError];
        if(!docError)
        {
            if(children && [children count]>0)
            {
                GDataXMLElement *rootElement = (GDataXMLElement *)[children objectAtIndex:0];
                NSArray *nodearr = [rootElement children];
                for (int i = 0; i<[nodearr count]; i++) {
                    GDataXMLElement *element = (GDataXMLElement *)[nodearr objectAtIndex:i];
                    [resultDict setObject:[element stringValue] forKey:[element name]];
                }
            }
        }
    }
    [document release];
    return [resultDict autorelease];
}
相關文章
相關標籤/搜索