Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...