gSOAP DOM解析器问题

时间:2013-08-29 14:38:58

标签: c++ xml dom utf-8 gsoap

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

soapns.cpp:

#include <soap.nsmap>   

soap.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}
};
使用soapcpp2.exe实用程序生成

soapC.cpp, soap.H, soap.nsmap

main.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时,我会看到'Новая коллекция'而不是{{1}}。有人知道如何解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:2)

此问题已在gSOAP 2.8.16

中修复