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

时间:2014-11-19 09:01:59

标签: c++ xml rapidxml

我正在尝试使用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).

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:2)

如果您需要在此处明确指定模板参数:

rapidxml::print<std::string,wchar_t>(std::back_inserter(xml_as_string), doc);
你几乎肯定犯了一个错误。该功能应该在这样的自动模板arg检测的帮助下工作。

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

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

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

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

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

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