3

RapidXML 是用于在 c++ 中解析 XML 的可用库之一。为了获取值,我们可以使用类似的东西:

node->first_node("xmlnode")->value()

此命令返回 char* 数据类型。有什么方法可以将值读取为 Unicode,以便我可以在 WCHAR 或 wstring 变量中分配它?

4

3 回答 3

1

从手册

RapidXml 与字符类型无关,可以处理窄字符和宽字符。当前版本不完全支持 UTF-16 或 UTF-32,因此使用宽字符有些不便。但是,如果数据的字节序与机器的字节序匹配,它应该成功解析包含 UTF-16 或 UTF-32 的 wchar_t 字符串。

所以我只使用以下内容:

#include <rapidxml/rapidxml.hpp>
typedef rapidxml::xml_node<wchar_t> const *      xml_node_cptr;
typedef rapidxml::xml_node<wchar_t> *            xml_node_ptr;
typedef rapidxml::xml_attribute<wchar_t> const * xml_attribute_cptr;
typedef rapidxml::xml_attribute<wchar_t> *       xml_attribute_ptr;
typedef rapidxml::xml_document<wchar_t>          xml_doc;

请注意,如果这样做,所有参数都将是 wchar_t,因此对 first_node() 的调用也需要 wchar_t。IE

node->first_node(L"xmlnode")->value()
于 2013-10-04T01:04:58.533 回答
1

在这里,您需要将 str 转换为 wstr。你可以使用这个标准标准

#include <string>
#include <codecvt>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

std::string strSample; // convert str to wstr
std::wstring wstrValue = converter.from_bytes(strSample);

std::wstring wstrSample; // convert wstr to str
std::string strValue = converter.to_bytes(wstrSample);

希望这有帮助

于 2017-12-06T11:45:07.273 回答
-1

另一种解决方案是使用以下功能: http: //msmvps.com/blogs/gdicanio/archive/2010/01/04/conversion-between-unicode-utf-16-and-utf-8-in-c-win32 .aspx

CStringW ConvertUTF8ToUTF16( __in const CHAR * pszTextUTF8 )
{
    //
    // Special case of NULL or empty input string
    //
    if ( (pszTextUTF8 == NULL) || (*pszTextUTF8 == '\0') )
    {
        // Return empty string
        return L"";
    }


    //
    // Consider CHAR's count corresponding to total input string length,
    // including end-of-string (\0) character
    //
    const size_t cchUTF8Max = INT_MAX - 1;
    size_t cchUTF8;
    HRESULT hr = ::StringCchLengthA( pszTextUTF8, cchUTF8Max, &cchUTF8 );
    if ( FAILED( hr ) )
    {
        AtlThrow( hr );
    }

    // Consider also terminating \0
    ++cchUTF8;

    // Convert to 'int' for use with MultiByteToWideChar API
    int cbUTF8 = static_cast<int>( cchUTF8 );


    //
    // Get size of destination UTF-16 buffer, in WCHAR's
    //
    int cchUTF16 = ::MultiByteToWideChar(
        CP_UTF8,                // convert from UTF-8
        MB_ERR_INVALID_CHARS,   // error on invalid chars
        pszTextUTF8,            // source UTF-8 string
        cbUTF8,                 // total length of source UTF-8 string,
                                // in CHAR's (= bytes), including end-of-string \0
        NULL,                   // unused - no conversion done in this step
        0                       // request size of destination buffer, in WCHAR's
        );
    ATLASSERT( cchUTF16 != 0 );
    if ( cchUTF16 == 0 )
    {
        AtlThrowLastWin32();
    }


    //
    // Allocate destination buffer to store UTF-16 string
    //
    CStringW strUTF16;
    WCHAR * pszUTF16 = strUTF16.GetBuffer( cchUTF16 );

    //
    // Do the conversion from UTF-8 to UTF-16
    //
    int result = ::MultiByteToWideChar(
        CP_UTF8,                // convert from UTF-8
        MB_ERR_INVALID_CHARS,   // error on invalid chars
        pszTextUTF8,            // source UTF-8 string
        cbUTF8,                 // total length of source UTF-8 string,
                                // in CHAR's (= bytes), including end-of-string \0
        pszUTF16,               // destination buffer
        cchUTF16                // size of destination buffer, in WCHAR's
        );
    ATLASSERT( result != 0 );
    if ( result == 0 )
    {
        AtlThrowLastWin32();
    }

    // Release internal CString buffer
    strUTF16.ReleaseBuffer();

    // Return resulting UTF16 string
    return strUTF16;
}
于 2014-01-22T15:30:32.590 回答