4

Boost 関数read_xmlおよびwrite_xmlを使用して、XML ファイルを読み書きしようとしています。XML ファイルの元のエンコーディングは「windows-1252」ですが、読み取り/書き込み操作の後、エンコーディングは「utf-8」になりました。

これは XML の元のファイルです。

<?xml version="1.0" encoding="windows-1252" standalone="no" ?>
<lot>
  <name>Lot1</name>
  <lot_id>123</lot_id>
  <descr></descr>
  <job>
    <name>TEST</name>
    <num_items>2</num_items>
    <item>
      <label>Item1</label>
      <descr>Item First Test</descr>
    </item>
    <item>
      <label>Item2</label>
      <descr>Item Second Test</descr>
    </item>
  </job>
</lot>

そして、これは出力のものです:

<?xml version="1.0" encoding="utf-8"?>
<lot>
    &#10;&#10;  &#10;&#10;  &#10;&#10;  &#10;&#10;  &#10;&#10;  &#10;&#10;  &#10;&#10;  &#10;&#10;
  <name>Lot1</name>
  <lot_id>123</lot_id>
  <descr></descr>
  <job>
    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;    &#10;  
    <name>TEST</name>
    <num_items>2</num_items>
    <item>
      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;    
      <label>Item1</label>
      <descr>Item First Test</descr>
    </item>
    <item>
      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;      &#10;    
      <label>Item2</label>
      <descr>Item Second Test</descr>
    </item>
  </job>
</lot>

これは私の C++ コードです (単なるテスト コードです):

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;

ptree xmlTree;
read_xml(FILE_XML, xmlTree);

for (auto it = xmlTreeChild.begin(); it != xmlTreeChild.end();)
{
    std::string strItem = it->first.data();
    if (strcmp(strItem.c_str(), "item") == 0)
    {
        std::string strLabel = it->second.get_child("label").data();
        if (strcmp(strLabel.c_str(), "item3") != 0)
        {
            it = xmlTreeChild.erase(it);
        }
    }       
    ++it;
}

auto settings = boost::property_tree::xml_writer_make_settings<std::string>('\t', 1);
write_xml(FILE_XML, xmlTree, std::locale(), settings);

元のファイルと同じエンコーディングを使用して、ファイルを読み書きし直す必要があります。次を使用して、ロケール設定も変更しようとしました。

std::locale newlocale1("English_USA.1252");
read_xml(FILE_XML, xmlTree, 0, newlocale1);
...
auto settings = boost::property_tree::xml_writer_make_settings<std::string>('\t', 1);
write_xml(FILE_XML, xmlTree, newlocale1, settings);

しかし、私は同じ結果を得ました。

Boost 関数を使用して、元のファイル エンコーディングを使用して読み書きできるようにするにはどうすればよいですか?

ありがとうございました

4

2 に答える 2

1

ライター設定を介してエンコーディングを渡すことができます。

auto settings = boost::property_tree::xml_writer_make_settings<std::string>(
    '\t', 1, "windows-1252");

もちろん、キー/値が実際に latin1/cp1252 と互換性があることを確認してください (これは、ソース ファイルからすべての情報を読み取る限り意味があります。ただし、ユーザー入力をプロパティ ツリー ノードに割り当てる場合などには注意が必要です。最初に入力エンコーディングから cp1252 に変換する必要がある場合があります)。

Live On Coliru

于 2015-01-09T19:44:21.560 に答える