#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;
}
This program prints OK and writes XML file as expected:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Name>Alex</Name>
</Data>
Now I replace s
value with non-ASCII characters:
//wstring s(L"Alex");
wstring s(L"Алекс");
When the program is executed, it prints: write error
, and XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Name>
How can I fix this? I need to write non-ASCII data to XML file, using Boost property tree.