#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;
}
这个程序打印确定,按照预期写入XML文件:写作Unicode字符串为XML加速属性树
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Name>Alex</Name>
</Data>
现在我更换s
值与非ASCII字符:
//wstring s(L"Alex");
wstring s(L"Алекс");
执行程序时,它会打印:write error
,而XML文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Name>
我该如何解决这个问题?我需要使用Boost属性树将非ASCII数据写入XML文件。