Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

c++ - How to convert pugi::char_t* to string

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...