3

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

serializator
  • 33
  • 1
  • 3

2 Answers2

0

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I'm not a fan of these compiler macros. Wouldn't it be better to provide an identity-overload `as_wide(wchar_t*)` and `as_utf8(char*)` so that the correct overload would be selected automatically based on the settings? – Kerrek SB Aug 21 '11 at 20:17
  • Define "the correct overload". As you can see above, it's not clear whether you want to end up with a `std::string` or a `std::wstring`; you can't overload on return type, and you can't expect the library to divine what you wanted either. – Lightness Races in Orbit Aug 21 '11 at 20:19
  • `as_wide` always returns `wstring`, and `as_utf8` always returns `string`. We just overload on the argument type to make it work even in the case where there's nothing to do. – Kerrek SB Aug 21 '11 at 20:20
  • @Kerrek: Oh, `as_wide` already exists? OK then, yea. :) Though what I don't like about that is that you have to overload on `char*` and `wchar_t*` (and assume that these will not change), rather than on the documented `PUGIXML_WCHAR_MODE`, which is clearly what the developers intend for us to use for switching on this. It adds a breakage point. – Lightness Races in Orbit Aug 21 '11 at 20:25
  • Wait, if you're going to store it in a `std::[w]string`, you still need the macro to decide which one, unless you want to duplicate _all_ your code (or create your own wrapping template type, I guess). – Lightness Races in Orbit Aug 21 '11 at 20:27
  • For internal use, you can store everything in a `pugi::string_t`. I suppose you'll only run the conversion at well-defined interfaces. – Kerrek SB Aug 21 '11 at 20:33
0

you can use stringstream also,

std::stringstream ss;
ss << it->name();

std::string strValue = ss.str();

ps: Don't forget to include

#include <sstream>
Renju Ashokan
  • 428
  • 6
  • 18