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]);