在Linux中,我有类似的东西:
boost::property_tree::xml_parser::read_xml(argv[1], pt);
// huge parsing
for (auto const& itemNode : rootNode2.second) {
const pt::ptree& dealAttributes = itemNode.second.get_child("<xmlattr>", empty_ptree());
for (auto const& dealAttr : dealAttributes)
{
const std::string& attrName = dealAttr.first;
const std::string& attrVal = dealAttr.second.data();
我该如何定义这个常数?似乎在Linux默认的俄语enconding不是Windows-1251所以我的比较失败了。
以下是xml示例:
<?xml version="1.0" encoding="cp1251"?>
<root>
<item accounting_currency_code="RUB" board_name="ФБ Т+2"
broker_commission="71.85" broker_ref="3/00/2"
conclusion_date="2013-09-11T00:00:00" conclusion_time="2013-09-11T11:04:35"
deal_no="480" execution_date="2013-09-12T00:00:00" price="144.700000"
price_currency_code="RUB" request_no="1976"
security_grn_code="1-02-008-A" security_name="ГАЗПРОМ ао"
sell_qnty="5000.00000000" volume_currency="718500.00"
volume_rur="718500.00"/>
</root>
答案 0 :(得分:1)
好的,这是我使用“随机样本”“成功”的步骤:
称它为例如input.xml
并确保它已保存在cp2151中:
<?xml version="1.0" encoding="windows-1251"?>
<root>
<deal id="1" silly="е">
hello
</deal>
</root>
那就是U + 0435(姓名:CYRILLIC SMALL LETTER IE)。
对我来说,我必须
ru_RU.CP1251 CP1251
,然后sudo dpkg-reconfigure locales
使用充当特定区域设置的read_xml
和write_xml
。
使用Boost Locale生成知道字符集转换 facets 的语言环境实例:
boost::locale::generator gen;
auto loc = gen.generate("ru_RU.CP1251");
注意我必须采取特殊预防措施来验证'debug.xml'文件的内容。我的vim误检测了编码,将其显示为latin1(
"å"
代替)。我用了:ed ++enc=cp1251 debug.xml
强制使用正确的代码页。
<强> Live On Coliru 强>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/locale.hpp>
#include <boost/locale/generator.hpp>
#include <iostream>
#include <fstream>
using boost::property_tree::ptree;
static ptree const& empty_ptree() {
static ptree _instance;
return _instance;
}
int main(int argc, char** argv) {
assert(argc>1);
boost::locale::generator gen;
auto loc = gen.generate("ru_RU.CP1251");
ptree pt;
read_xml(argv[1], pt, 0, loc);
ptree::value_type& rootNode2 = *pt.begin();
// huge parsing
for (auto const& itemNode : rootNode2.second) {
const ptree& dealAttributes = itemNode.second.get_child("<xmlattr>", empty_ptree());
for (auto const& dealAttr : dealAttributes)
{
const std::string& attrName = dealAttr.first;
const std::string& attrVal = dealAttr.second.data();
std::cout << "Attribute '" << attrName << "' hath value "; // '" << attrVal << "'\n";
int pos = 1;
for (uint8_t ch : attrVal) // prevent sign-extension
{
if (pos++ == 8) {
std::cout << '\n';
pos = 1;
}
std::cout << std::hex << std::setw(2) << std::setfill('0') << std::showbase << static_cast<int>(ch) << " ";
}
std::cout << "\n";
}
}
auto settings = boost::property_tree::xml_writer_make_settings<std::string>(' ', 4, "windows-1251");
//boost::property_tree::xml_parser::write_xml_element(ofs, "root", pt, 0, settings);
write_xml("debug.xml", pt, loc, settings);
}
它在Coliru上运行,包括语言环境支持(kudos,Coliru!),并且十六进制转储debug.xml
进行验证:
Attribute 'id' hath value 0x31
Attribute 'silly' hath value 0xe5
0000000: 3c3f 786d 6c20 7665 7273 696f 6e3d 2231 <?xml version="1
0000010: 2e30 2220 656e 636f 6469 6e67 3d22 7769 .0" encoding="wi
0000020: 6e64 6f77 732d 3132 3531 223f 3e0a 3c72 ndows-1251"?>.<r
0000030: 6f6f 743e 0a20 2020 200d 2623 3130 3b20 oot>. .
0000040: 200d 2623 3130 3b0a 2020 2020 3c64 6561 . . <dea
0000050: 6c20 6964 3d22 3122 2073 696c 6c79 3d22 l id="1" silly="
0000060: e522 3e0d 2623 3130 3b20 2020 2020 2020 .">.
0000070: 2068 656c 6c6f 0d26 2331 303b 2020 2020 hello.
0000080: 3c2f 6465 616c 3e0a 3c2f 726f 6f74 3e0a </deal>.</root>.
如您所见,0x22 0xe5 0x22
是cp1251中"е"
的正确十六进制表示