xml_node<wchar_t> I'm using rapid xml to print the XML document to a file

1.1k Views Asked by At

I'm trying to print a xml to a file using rapidxml . But the values are in wstring. So i've used templated version of it by attaching xml_node wchar_t instead of specialization of the xml_node . But When i'm doing this:

std::string xml_as_string;
rapidxml::print<std::string,wchar_t>(std::back_inserter(xml_as_string), doc); ///i'm not very sure of this line . This line only gives a error 
// i tried this   "rapidxml::print<t>" also i'm getting an error .

//Save to file
std::ofstream file_stored("C:\\Logs\\file_stored.xml");
file_stored << doc;
file_stored.close();
doc.clear();

It throws a error stating Error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rapidxml::xml_document<Ch>' (or there is no acceptable conversion).

Any help will be appreciated . thanks

1

There are 1 best solutions below

3
On BEST ANSWER

If you need to explicitly specify the template arguments here:

rapidxml::print<std::string,wchar_t>(std::back_inserter(xml_as_string), doc);

you almost certainly make a mistake. The function is supposed to work with the help of automatic template arg detection like this.

rapidxml::xml_node<wchar_t> doc(...); // Do some init here
std::wstring xml_as_string;

rapidxml::print(std::back_inserter(xml_as_string), doc);

The point is that both args have to be wchar_t based otherwise the templated function would not match. So in the first step you may only output to an wstring or std::basic_string<wchar_t> to be more specific. Or to an output iterator into a wchar_t output.

If you want to end up with a char based encoding (e.g utf8) you may either use a wchar file output stream and imbue(...) it like:

// Not properly checked if this compiles
std::wofstream of("bla.xml");
of.imbue(std::locale("en_US.utf8"));
of << xml_as_string;

or you may use some iconv(...) based solution. You may of course us a file output iterator directly on the wofstream.