xml_node<wchar_t> 我正在使用快速 xml 将 XML 文档打印到文件



我正在尝试使用 rapidxml 将 xml 打印到文件中。但是这些值是wstring.所以我通过附加来使用它的模板化版本xml_node wchar_t而不是xml_node的专业化.但是当我这样做时:

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();

它会抛出一个错误,指出错误:

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

任何帮助将不胜感激。

谢谢

如果需要在此处显式指定模板参数:

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

你几乎肯定会犯一个错误。该函数应该在自动模板参数检测的帮助下工作,就像这样。

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

关键是两个参数都必须基于wchar_t,否则模板化函数将不匹配。因此,在第一步中,您只能输出到更具体的wstringstd::basic_string<wchar_t>。或者将输出迭代器转换为wchar_t输出。

如果你想最终得到一个基于字符的编码(例如utf8),你可以使用wchar文件输出流并灌输(...)它,就像:

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

或者你可以使用一些基于iconv(...)的解决方案。当然,您可以直接在 wofstream 上使用文件输出迭代器。

最新更新