【发布时间】:2014-03-06 09:22:20
【问题描述】:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>
using namespace std;
int main()
{
wstring s(L"Alex");
boost::property_tree::wptree mainTree;
boost::property_tree::wptree dataTree;
dataTree.put(L"Name", s);
mainTree.add_child(L"Data", dataTree);
boost::property_tree::xml_writer_settings<wchar_t> w(L' ', 3);
try
{
write_xml("Data.xml", mainTree, std::locale(), w);
}
catch(boost::property_tree::xml_parser_error& error)
{
cout << error.message().c_str() << endl;
return 1;
}
cout << "OK" << endl;
return 0;
}
这个程序打印OK并按预期写入XML文件:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Name>Alex</Name>
</Data>
现在我用非 ASCII 字符替换 s 值:
//wstring s(L"Alex");
wstring s(L"Алекс");
程序执行时打印:write error,XML文件如下:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Name>
我该如何解决这个问题?我需要使用 Boost 属性树将非 ASCII 数据写入 XML 文件。
【问题讨论】:
标签: c++ xml boost boost-propertytree