我尝试使用 gSOAP 2.8.10 DOM 解析器来解析包含 UTF8 编码的西里尔文文本的简单 XML。我创建了 VC++ 控制台应用程序,添加到项目soapC.cpp
和soapns.cpp
.
肥皂剧.cpp:
#include <soap.nsmap>
肥皂.nsmap:
#include "soapH.h"
SOAP_NMAC struct Namespace namespaces[] =
{
{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org /*/soap-envelope", NULL},
{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL},
{"xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/*/XMLSchema-instance", NULL},
{"xsd", "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/*/XMLSchema", NULL},
{"ns2", "http://schemas.microsoft.com/2003/10/Serialization/", NULL, NULL},
{"ns1", "http://asp.net/ApplicationServices/v200", NULL, NULL},
{"ns3", "http://tempuri.org/", NULL, NULL},
{NULL, NULL, NULL, NULL}
};
soapC.cpp, soap.H, soap.nsmap
是使用 soapcpp2.exe 实用程序生成的。
主.cpp:
#include <stdsoap2.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <tchar.h>
void print_in_hex(const std::string& str)
{
std::string::const_iterator ch;
for(ch = str.begin(); ch != str.end(); ++ch)
{
std::cout << std::hex <<
std::setw(2) << std::setfill('0') << std::uppercase <<
static_cast<unsigned int>(static_cast<unsigned char>(*ch)) << " ";
}
std::cout << std::endl;
}
// Sample XML content
const std::string Xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<entry>\
<properties>\
<Id>a8a4cf87-9497-4078-9166-0737a55ca7fc</Id>\
<Name>\xD0\x9D\xD0\xBE\xD0\xB2\xD0\xB0\xD1\x8F\x20\xD0\xBA\
\xD0\xBE\xD0\xBB\xD0\xBB\xD0\xB5\xD0\xBA\xD1\x86\xD0\xB8\xD1\x8F</Name>\
</properties>\
</entry>";
const std::string correctName = "\xD0\x9D\xD0\xBE\xD0\xB2\xD0\xB0\xD1\x8F\x20\xD0\xBA\
\xD0\xBE\xD0\xBB\xD0\xBB\xD0\xB5\xD0\xBA\xD1\x86\xD0\xB8\xD1\x8F";
int _tmain(int argc, _TCHAR* argv[])
{
std::stringstream inputStream;
inputStream.str(Xml);
struct soap_dom_element entry(soap_new());
soap_set_mode(entry.soap, SOAP_DOM_TREE | SOAP_C_UTFSTRING);
inputStream >> entry;
soap_dom_element_iterator it = entry.find( NULL, "Name");
if( it != entry.end() )
{
std::cout << "Original content:" << std::endl;
print_in_hex(correctName);
std::string name = (*it).data;
std::cout << "Parsed content:" << std::endl;
print_in_hex(name);
}
return 0;
}
输出:
Original content:
D0 9D D0 BE D0 B2 D0 B0 D1 8F 20 D0 BA D0 BE D0 BB D0 BB D0 B5 D0 BA D1 86 D0 B8 D1 8F
Parsed content:
C3 90 9D D0 BE D0 B2 D0 B0 D1 8F 20 D0 BA D0 BE D0 BB D0 BB D0 B5 D0 BA D1 86 D0 B8 D1 8F
当从流中读取 XML 时,gSOAP 放置两个字节而不是标记原始内容的0xC3 0x90
第一个字节。结果,我看到的不是文本从 UTF8 解码为 Windows-1251 的时间。有谁知道如何解决这个问题?谢谢!0xD0
<Name>
'??овая коллекция'
'Новая коллекция'