织梦CMS - 轻松建站从此开始!

罗索实验室

IOS WSDL2OBJC如何与CXF对接

落鹤生 发布于 2012-02-16 15:03 点击:次 
首先, WSDL2OBJC直接生成的代码是无法正确发送CXF能解析的数据包的,我懒得去看WSDL2OBJC的源码, 但是由其生成的源码看看倒是ok的
TAG:

首先, 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

 

(Khan)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www1.rosoo.net/a/201202/15724.html]
本文出处:博客园 作者:Khan 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容