Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

路漫漫,长修远,我们不能没有钱
随笔 - 172, 文章 - 0, 评论 - 257, 引用 - 0
数据加载中……

IOS WSDL2OBJC如何与CXF对接

首先, WSDL2OBJC直接生成的代码是无法正确发送CXF能解析的数据包的

我懒得去看WSDL2OBJC的源码, 但是由其生成的源码看看倒是ok的

我wsdl的服务名是HotelPortService
生成的文件中有个HotelPortServiceSvc.m, 要修改的地方全部集中在这个文件
首先, 找到如下代码的位置, 替换掉

- (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements

{

    xmlDocPtr doc;

doc = xmlNewDoc((const xmlChar*)XML_DEFAULT_VERSION);

if (doc == NULL) {

NSLog(@"Error creating the xml document tree");

return @"";

}

xmlNodePtr root = xmlNewDocNode(doc, NULL, (const xmlChar*)"Envelope", NULL);

xmlDocSetRootElement(doc, root);

xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://schemas.xmlsoap.org/soap/envelope/", (const xmlChar*)"soap");

xmlSetNs(root, soapEnvelopeNs);

xmlNsPtr xslNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/1999/XSL/Transform", (const xmlChar*)"xsl");

xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar*)"xsi");

xmlNewNsProp(root, xslNs, (const xmlChar*)"version", (const xmlChar*)"1.0");

xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema", (const xmlChar*)"xs");

xmlNewNs(root, (const xmlChar*)"http://port.ekezhan.com/", (const xmlChar*)"HotelPortServiceSvc");//字符串部分根据服务名不同而不同

if((headerElements != nil) && ([headerElements count] > 0)) {

xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);

xmlAddChild(root, headerNode);

for(NSString *key in [headerElements allKeys]) {

id header = [headerElements objectForKey:key];

xmlAddChild(headerNode, [header xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);

}

}

if((bodyElements != nil) && ([bodyElements count] > 0)) {

xmlNodePtr bodyNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Body", NULL);

xmlAddChild(root, bodyNode);

for(NSString *key in [bodyElements allKeys]) {

id body = [bodyElements objectForKey:key];

xmlAddChild(bodyNode, [body xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);

}

}

xmlChar *buf;

int size;

xmlDocDumpFormatMemory(doc, &buf, &size, 1);

NSString *serializedForm = [NSString stringWithCString:(const char*)buf encoding:NSUTF8StringEncoding];

xmlFree(buf);

xmlFreeDoc(doc);

return serializedForm;

}


替换成:

- (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements

{

    xmlDocPtr doc;

doc = xmlNewDoc((const xmlChar*)XML_DEFAULT_VERSION);

if (doc == NULL) {

NSLog(@"Error creating the xml document tree");

return @"";

}

xmlNodePtr root = xmlNewDocNode(doc, NULL, (const xmlChar*)"Envelope", NULL);

xmlDocSetRootElement(doc, root);

xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://schemas.xmlsoap.org/soap/envelope/", (const xmlChar*)"soapenv");//此处修改

xmlSetNs(root, soapEnvelopeNs);

//xmlNsPtr xslNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/1999/XSL/Transform", (const xmlChar*)"xsl");

//xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar*)"xsi");

//xmlNewNsProp(root, xslNs, (const xmlChar*)"version", (const xmlChar*)"1.0");

//xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema", (const xmlChar*)"xs");

    

xmlNewNs(root, (const xmlChar*)"http://port.ekezhan.com/", (const xmlChar*)"port");

if((headerElements != nil) && ([headerElements count] > 0)) {

xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);

xmlAddChild(root, headerNode);

for(NSString *key in [headerElements allKeys]) {

id header = [headerElements objectForKey:key];

xmlAddChild(headerNode, [header xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);

}

}

//此处修改

else

{  

xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);

xmlAddChild(root, headerNode);

}

if((bodyElements != nil) && ([bodyElements count] > 0)) {

xmlNodePtr bodyNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Body", NULL);

xmlAddChild(root, bodyNode);

for(NSString *key in [bodyElements allKeys]) {

id body = [bodyElements objectForKey:key];

xmlAddChild(bodyNode, [body xmlNodeForDoc:doc elementName:key elementNSPrefix:@"port"]);//此处修改

}

}

xmlChar *buf;

int size;

xmlDocDumpFormatMemory(doc, &buf, &size, 1);

NSString *serializedForm = [NSString stringWithCString:(const char*)buf encoding:NSUTF8StringEncoding];

xmlFree(buf);

xmlFreeDoc(doc);

return serializedForm;

}


替换全部

elementNSPrefix:@"HotelPortServiceSvc" 

elementNSPrefix:nil  //字符串部分根据服务名不同而不同



替换全部

nodeName = [NSString stringWithFormat:@"%@:%@", @"HotelPortServiceSvc", elName];//字符串部分根据服务名不同而不同

 

nodeName = [NSString stringWithFormat:@"%@", elName];




然后再编译运行. 如果没有意外, 就能获得正确的结果了. 文档如有错漏, 欢迎指正补全



测试环境 

Mac OS 10.6.8,  xcode 4.0.1,  WSDL2ObjC 0.7 pre1

posted on 2011-11-04 17:36 Khan 阅读(1759) 评论(2)  编辑 收藏 引用 所属分类: 跨平台开发

评论

# re: IOS WSDL2OBJC如何与CXF对接  回复  更多评论   

比较讨厌d地方是, 每次发布的webservice变动. 都需要重复一次, 不过仍旧比人肉解析xml要省事儿
2011-11-04 17:41 | Khan's Notebook

# re: IOS WSDL2OBJC如何与CXF对接  回复  更多评论   

我只想说太给力了。。解决了我一周都没有解决的问题,webservice交互问题,忙了一周多都没忙出来。。给力
2014-09-11 14:59 | 左凌风

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理