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