Boost 读/写 XML 文件 : how to change the characters encoding?

标签 boost xml-parsing xml-encoding

我正在尝试使用 Boost 函数读取/写入 XML 文件 read_xml write_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 函数使用原始文件编码进行读写?

谢谢

最佳答案

您可以通过编写器设置传递编码:

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

当然,确保键/值实际上与 latin1/cp1252 兼容(只要您从源文件中读取所有信息,这是有道理的;但是在将用户输入分配给属性树节点时必须小心;您可能需要先从输入编码转换为 cp1252)。

Live On Coliru

关于Boost 读/写 XML 文件 : how to change the characters encoding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864220/

相关文章:

c++ - 有没有办法截断打开的文件流?

c++ - 使用 boost.program_options 处理 '-'

java - 从 XML 中多次提取数据的最佳且优化的方法是什么?

javascript - 使用 AJAX 解析 XML 数据

java - 使用奇怪的标签编译样式表 XSL <a :tag>

c# - 我如何使用 XmlDocument.Save() 将 ="us-ascii"编码为数字字符实体而不是问号?

c++ - 使用 QMathGL 绘制实时数据?

c++ - BGL : get vertex descriptor with data

java - VTD-XML : XMLModifier. output() 抛出 IndexOutOfBoundsException