gSOAP在VC2008中的使用

 
      gSOAP工具包是一个提供SOAP/XML Web services和通用(non-SOAP) C/C++ XML数据绑定的开源C/C++软件开发工具包。

      从 http://sourceforge.net/projects/gsoap2/files/下载最新的gSOAP包,当前最新版本为2.8.8。解压之后,在目录"\gsoap-2.8\gsoap\bin\win32"下有两个文件,分别为:
  • wsdl2h.exe        该工具转换WSDL和XSD文件到带有注释的C/C++定义文件。
  • soapcpp2.exe    该工具从带有注释的C/C/++定义文件生成RPC代码和XML序列化。
命令行介绍
使用:wsdl2h [-a] [-c] [-d] [-e] [-f] [-g] [-h] [-I path] [-i] [-j] [-k] [-l] [-m] [-n name] [-N name] [-p|-P] [-q name] [-r proxyhost[:port[:uid:pwd]]] [-s] [-t typemapfile] [-u] [-v] [-w] [-W] [-x] [-y] [-z#] [-_] [-o outfile.h] infile.wsdl infile.xsd http://www... ...
​wsdl2h选项
Option Description
-a generate indexed struct names for local elements with anonymous types
为匿名类型的本地元素生成带索引结构名称
-c
generate C source code
生成C源代码 
-d
use DOM to populate xs:any and xsd:anyType elements
使用DOM填充 xs:any 和 xsd:anyType 元素
-e don't qualify enum names
This option is for backward compatibility with gSOAP 2.4.1 and earlier.
The option does not produce code that conforms to WS-I Basic Profile 1.0a.
不修饰枚举名称
-f generate flat C++ class hierarchy for schema extensions
-g generate global top-level element declarations
生成全局顶层元素声明
-h print help information
打印帮助信息
-i don't import
不导入
-I path use path to locate source files for #import
让#import使用路径path查找源文件
-j don't generate SOAP_ENV__Header and SOAP_ENV__Detail definitions
-k don't generate SOAP_ENV__Header mustUnderstand qualifiers
-l include license information in output
输出时包括许可证信息
-m use xsd.h module to import primitive types
使用xsd.h模块导入私钥
-n name use name as the base namespace prefix name instead of ns
使用名称name代替ns来作为基本的命名空间前缀名
-N name use name as the base namespace prefix name for service namespaces
使用名称name代替服务命名空间来作为基本的命名空间前缀名
-o file output to file
输出到file文件
-p create polymorphic types with C++ inheritance hierarchy from base xsd__anyType
从基本的xsd__anyType创建带C++中的继承层次结构的多态类型
-P don't create polymorphic types with C++ inheritance hierarchy from xsd__anyType
This is automatically performed when WSDL contains polymorphic definitions
-q name use name for the C++ namespace of all declarations
使用名称name作为C++命名空间的所有声明
-r host[:port[:uid:pwd]] connect via proxy host, port, and proxy credentials
连接通过代理主机,端口和代理凭证
-s don't generate STL code (no std::string and no std::vector)
不要生成STL代码
-t file use type map file instead of the default file typemap.dat
使用type map文件代替默认的typemap.dat
-u don't generate unions
不要生成联合
-v verbose output
详细输出
-w always wrap response parameters in a response struct
总是封装在一个响应结构的响应参数
-W suppress warnings
禁止警告
-x don't generate _XML any/anyAttribute extensibility elements
-y generate typedef synonyms for structs and enums
-z1 compatibility with 2.7.6e: generate pointer-based arrays
-z2 compatibility with 2.7.15: qualify element/attribute referenced members
-_ don't generate _USCORE (replace with UNICODE _x005f)
-? print help information
打印帮助信息

使用: soapcpp2 [-1|-2] [-C|-S] [-T] [-L] [-a] [-b] [-c] [-d path] [-e] [-f N] [-h] [-i] [-I path;path;...] [-k] [-l] [-m] [-n] [-p name] [-s] [-t] [-u] [-v] [-w] [-x] [-y] [infile]
soapcpp2 选项

Option Description
-1 Use SOAP 1.1 namespaces and encodings (default)
-2 Use SOAP 1.2 namespaces and encodings
-C Generate client-side code only
仅生成客户端代码
-S Generate server-side code only
仅生成服务器端代码
-T Generate server auto-test code
生成服务器自动测试代码
-L Do not generate soapClientLib/soapServerLib
不要生成soapClientLib/soapServerLib
-a Use value of SOAPAction HTTP header to dispatch method at server side
在服务器端使用SOAPAction HTTP头的值去调度方法
-b serialize byte arrays char[N] as string
-c Generate pure C code
生成纯C代码
-d < path > Save sources in directory specified by < path >
保存源代码到指定的目录<path>里
-e Generate SOAP RPC encoding style bindings
-f N File split of N XML serializer implementations per file
-h Print a brief usage message
打印一个简短的用法消息
-i Generate service proxies and objects inherited from soap struct
从SOAP结构生成服务代理和继承的对象
-j Generate C++ service proxies and objects that can share a soap struct
-I < path > Use < path > (or paths separated with `:') for #import
-k generate data structure walkers (experimental)
-l Generate linkable modules (experimental)
-m Generate Matlabtm code for MEX compiler
-n When used with -p, enables multi-client and multi-server builds:
  Sets compiler option WITH_NONAMESPACES, see Section 9.11
  Saves the namespace mapping table with name < name > _namespaces instead of namespaces
  Renames soap_serve() into < name > _serve() and soap_destroy() into < name > _destroy()
-p < name > Save sources with file name prefix < name > instead of "soap"
保存源代码文件的前缀名<name>代替"soap"
-q < name > Use name for the C++ namespace of all declarations
使用<name>做为C++所有声明的命名空间
-s Generates deserialization code with strict XML validation checks
生成具有严格的XML验证检查的反序列化代码
-t Generates code to send typed messages (with the xsi:type attribute)
-u uncomment comments in WSDL/schema output by suppressing XML comments
-v Display version info
显示版本信息
-w
Do not generate WSDL and schema files
不生成WSDL和schema文件
-x Do not generate sample XML message files
不生成示例XML消息文件
-y include C/C++ type access information in sample XML messages

下面在VC2008中测试使用:
1.新建基于对话框工程;
2.工程目录下新建文件夹,名称为:CalcSoap,拷贝"...\gsoap-2.8\gsoap"目录下的stdsoap2.h、stdsoap2.cpp到此文件夹;
3.在命令提示符下,输入以下内容:

wsdl2h.exe -t ..\..\typemap.dat -s -o "E:\Project\TestgSOAP\CalcSoap\calc.h" http://www.genivia.com/calc.wsdl 
gSOAP在VC2008中的使用_第1张图片
soapcpp2.exe -C -L -j -d "E:\Project\TestgSOAP\CalcSoap" -w -x "E:\Project\TestgSOAP\CalcSoap\calc.h" 
gSOAP在VC2008中的使用_第2张图片

将soapcalcProxy.h、stdsoap2.h、soapC.cpp、soapcalcProxy.cpp、stdsoap2.cpp加入到工程,右键选择soapC.cpp、soapcalcProxy.cpp、stdsoap2.cpp,点击"属性",如下图所示:
gSOAP在VC2008中的使用_第3张图片

去掉预编译头,如下图所示:
gSOAP在VC2008中的使用_第4张图片

在对话框实现文件,添加以下头文件:

# include "..\CalcSoap\soapcalcProxy.h"
# include "..\CalcSoap\calc.nsmap"

添加个测试按钮,按钮事件如下:

void CTestgSOAPDlg : :OnBnClickedButton1()
{
    calcProxy service;
     double result = 5. 0;
     if (service.add( 1. 0, 2. 0, result) == SOAP_OK)
    {
        TRACE(_T( "%lf"), result);
    } 
     else
    {
        TRACE(_T( "%lf"), result);
    }
    service.destroy();
}
调试运行成功。下面测试一下错误日志输出:
void CTestgSOAPDlg : :OnBnClickedButton1()
{
    calcProxy service;
     double result = 5. 0;
     if (service.div( 1. 0, 0. 0, result) == SOAP_OK)
    {
        TRACE(_T( "%lf"), result);
    } 
     else
    {
        TRACE( "%s", service.soap_fault_string()); //这里不要加_T宏
    }
    service.destroy(); //记得销毁
}
除以0的时候,就不会返回 SOAP_OK ,打印出失败信息,如下图所示:
 
 
扩展资料:
1.gSOAP 2.8.7 User Guide  http://www.cs.fsu.edu/~engelen/soapdoc2.html
2.gSOAP 快速使用步骤  http://blog.csdn.net/g5dsk/article/details/6733432
3.gSoap转换WebServices  http://blog.book41.net/?p=135
4.gSoap使用笔记 http://www.cnblogs.com/Lvkun/archive/2010/03/30/gSoap_Note.html

你可能感兴趣的:(xml,SOAP,Path,hierarchy,inheritance,NameSpaces)