boost读取utf-8编码xml文件

时间:2023-01-17 22:25:43

参照:http://www.cnblogs.com/qicosmos/p/3555668.html

解决boost读取utf-8 xml文件乱码问题.

代码如下

void io::read(const string& file)
{
try
{
wifstream f(file);
locale utf8Locale(locale("chs"), new codecvt_utf8<wchar_t>);
f.imbue(utf8Locale);
wptree pt;
read_xml(f, pt);
wstring w = pt.get<wstring>(L"class.name");
string a(w.begin(),w.end());
wcout.imbue(locale("chs"));
wcout << w;
}catch(exception e)
{
cout << "error: " << e.what();
}
}

 2014-06-07 00:22:26