GDataXMLNode是Google提供的用於XML資料處理的類集。該類集對libxml2-DOM處理方式進行了封裝,能對較小或中等的XML文檔進行讀寫操作且支援XPath文法。
獲得GDataXMLNode.h和GDataXMLNode.m檔案,進行如下操作將檔案匯入到Xcode的工程中:
1. 將GDataXMLNode.h/m檔案添加到工程中;
2. 向工程中增加“libxml2.dylib”庫;
3. 在工程的“Build Settings”頁中找到“Header Search Path”項,添加“/usr/include/libxml2”到其路徑中
添加“GDataXMLNode.h”檔案到任一標頭檔中,如工程能編譯通過,則說明GDataXMLNode添加成功。
以產生一段SOAP請求為例,展示該類集的使用。
要產生的SOAP請求:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.example.com/mobile/2010"><soap:Body><getProductAd/></soap:Body></soap:Envelope>
使用的代碼:
//SOAP Envelope GDataXMLElement *envelope = [GDataXMLElement elementWithName:@"SOAP-ENV:Envelope"]; GDataXMLNode *soapNS = [GDataXMLNode namespaceWithName:@"SOAP-ENV" stringValue:@"http://schemas.xmlsoap.org/soap/envelope/"]; GDataXMLNode *xsiNS = [GDataXMLNode namespaceWithName:@"xsi" stringValue:@"http://www.w3.org/2001/XMLSchema-instance"]; GDataXMLNode *xsdNS = [GDataXMLNode namespaceWithName:@"xsd" stringValue:@"http://www.w3.org/2001/XMLSchema"]; GDataXMLNode *defaultNS = [GDataXMLNode namespaceWithName:@"" stringValue:@"http://www.example/mobile/2010"]; NSArray *namespaces = [NSArray arrayWithObjects:xsiNS, xsdNS, soapNS, defaultNS, nil]; [envelope setNamespaces:namespaces]; //SOAP Body GDataXMLElement *body = [GDataXMLElement elementWithName:@"SOAP-ENV:Body"]; //SOAP Value GDataXMLElement *value = [GDataXMLElement elementWithName:@"getProductAd"]; [body addChild:value]; [envelope addChild:body]; //SOAP Document GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:envelope]; [doc setCharacterEncoding:@"utf-8"]; NSLog(@"doc = %@", [NSString stringWithCString:(const char *)[[doc XMLData] bytes] encoding:NSUTF8StringEncoding]);