gSOAP 将 XML 序列化为字符串

use*_*810 0 xml gsoap

无论如何,我可以打印序列化的 xml 消息(使用 gsoap)。

例如:

soap_serialize_ns1__Response(soap, &ns2__UpdatedResponse);
Run Code Online (Sandbox Code Playgroud)

我希望能够看到序列化 (xml) 的样子。

有谁知道怎么做?

Dr.*_* RE 5

无论如何,我可以打印序列化的 xml 消息(使用 gsoap)。

我希望能够看到序列化 (xml) 的样子。

要将序列化对象“打印”为 XML 中的字符串,您有两种选择,具体取决于您是使用 C 还是 C++ 进行编码。

使用 C 进行编码时,请执行以下操作:

struct soap *soap = soap_new();
...
const char *str = NULL;
soap->os = &str; // assign a string to write output to
soap_write_ns1__Response(soap, &response);
soap->os = NULL; // no longer writing to the string
printf("The XML is:%s\n", str);
...
soap_end(soap); // warning: this deletes str with XML too!
str = NULL;     // so make it NULL as good practice
soap_free(soap);
Run Code Online (Sandbox Code Playgroud)

使用 C++ 编码时,请执行以下操作:

soap *soap = soap_new();
...
std::stringstream ss;
soap->os = &ss; // assign a stringstream to write output to
soap_write_ns1__Response(soap, &response);
soap->os = NULL; // no longer writing to the stream
std::cout << "The XML is:\n" << ss.str();
...
soap_destroy(soap);
soap_end(soap);
soap_free(soap);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅其网站上的gSOAP XML 数据绑定