簡體   English   中英

在Linux中使用boost xml_parser如何讀取和比較Windows-1251文字?

[英]in linux using boost xml_parser how to read and compare windows-1251 literals?

在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();
  • 原始xml是Windows-1251。
  • attrVal是俄語文字
  • 我需要它與程序中的常量進行比較。

我應該如何定義這個常數? 在Linux中,默認的俄語encon似乎不是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>

好的,這是我通過“隨機樣本”“成功”執行的步驟:

  1. 創建樣本輸入:

    調用它,例如input.xml並確保將其保存在cp2151中:

     <?xml version="1.0" encoding="windows-1251"?> <root> <deal id="1" silly="е"> hello </deal> </root> 

    那是U + 0435(名稱:CYRILLIC小寫字母IE)。

  2. 配置系統以支持cp1251語言環境

    對我來說,我不得不

    • 編輯/var/lib/locales/supported.d/local添加ru_RU.CP1251 CP1251 ,然后
    • sudo dpkg-reconfigure locales
  3. 灌輸!

    使用具有特定語言環境的read_xmlwrite_xml

    使用Boost Locale生成知道字符集轉換方面的語言環境實例:

     boost::locale::generator gen; auto loc = gen.generate("ru_RU.CP1251"); 
  4. 利潤

注意我必須采取特殊的預防措施來驗證“ debug.xml”文件的內容。 我的vim錯誤地檢測到了編碼,顯示為latin1(代替"å" )。 我用了

 :ed ++enc=cp1251 debug.xml 

強制正確的代碼頁。

完整演示

生活在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上運行,包括語言環境支持(榮譽,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>.    .&#10; 
0000040: 200d 2623 3130 3b0a 2020 2020 3c64 6561   .&#10;.    <dea
0000050: 6c20 6964 3d22 3122 2073 696c 6c79 3d22  l id="1" silly="
0000060: e522 3e0d 2623 3130 3b20 2020 2020 2020  .">.&#10;       
0000070: 2068 656c 6c6f 0d26 2331 303b 2020 2020   hello.&#10;    
0000080: 3c2f 6465 616c 3e0a 3c2f 726f 6f74 3e0a  </deal>.</root>.

如您所見, 0x22 0xe5 0x220x22 0xe5 0x22"е"的正確十六進制表示形式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM