gpt4 book ai didi

c++ - gSOAP DOM 解析器问题

转载 作者:行者123 更新时间:2023-11-30 04:15:09 26 4
gpt4 key购买 nike

我尝试使用 gSOAP 2.8.10 DOM 解析器来解析包含 UTF8 编码的西里尔文本的简单 XML。我创建了 VC++ 控制台应用程序,添加到项目 soapC.cppsoapns.cpp .

soapns.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而不是第一个字节 0xD0 <Name>的原始内容标签。结果我看到 '??овая коллекция'而不是 'Новая коллекция'当文本从 UTF8 解码为 Windows-1251 时。有人知道如何解决这个问题吗?谢谢!

最佳答案

此问题已在 gSOAP 2.8.16 中修复

关于c++ - gSOAP DOM 解析器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com