IOS WSDL2OBJC如何與CXF對接
- (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");//字符串部分根據(jù)服務名不同而不同
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 //字符串部分根據(jù)服務名不同而不同
替換全部
nodeName = [NSString stringWithFormat:@"%@:%@", @"HotelPortServiceSvc", elName];//字符串部分根據(jù)服務名不同而不同
為
nodeName = [NSString stringWithFormat:@"%@", elName];
然后再編譯運行. 如果沒有意外, 就能獲得正確的結(jié)果了. 文檔如有錯漏, 歡迎指正補全
測試環(huán)境
posted on 2011-11-04 17:36 Khan 閱讀(1772) 評論(2) 編輯 收藏 引用 所屬分類: 跨平臺開發(fā)