How to convert pugi::char_t* to string

不想你离开。 提交于 2019-11-28 02:02:24

问题


Hi I'm using pugixml to process xml documents. I iterate through nodes using this construction

 pugi::xml_node tools = doc.child("settings");

    //[code_traverse_iter
    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
    {
        //std::cout << "Tool:";
        cout <<it->name();

    }

the problem is that it->name() returns pugi::char_t* and I need to convert it into std::string. Is it possible ?? I can't find any information on pugixml website


回答1:


According to the manual, pugi::char_t is either char or wchar_t, depending on your library configuration. This is so that you can switch between single bytes (ASCII or UTF-8) and double bytes (usually UTF-16/32).

This means you don't need to change it to anything. However, if you're using the wchar_t* variant, you will have to use the matching stream object:

#ifdef PUGIXML_WCHAR_MODE
std::wcout << it->name();
#else
std::cout << it->name();
#endif

And, since you asked, to construct a std::string or std::wstring from it:

#ifdef PUGIXML_WCHAR_MODE
std::wstring str = it->name();
#else
std::string str = it->name();
#endif

Or, for always a std::string (this is rarely what you want!):

#ifdef PUGIXML_WCHAR_MODE
std::string str = as_utf8(it->name());
#else
std::string str = it->name();
#endif

Hope this helps.

Source: A cursory glance at the "pugixml" documentation.



来源:https://stackoverflow.com/questions/6102698/how-to-convert-pugichar-t-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!