如何使用rapidxml读取Unicode XML值

时间:2013-08-29 14:50:36

标签: xml unicode xml-parsing rapidxml

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

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

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

3 个答案:

答案 0 :(得分:1)

From the manual

  

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。即。

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

答案 1 :(得分:1)

这里你需要将str转换为wstr。 你可以使用这个标准的std

#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);

希望这个帮助

答案 2 :(得分:-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;
}