问题
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