Boost读/写XML文件:如何更改字符编码?
我正在尝试使用Boost函数read\u-XML和write\u-XML读取/写入一个XML文件。 XML文件的原始编码是“windows-1252”,但在读/写操作之后,编码变成了“utf-8” 这是XML原始文件:Boost读/写XML文件:如何更改字符编码?,boost,xml-parsing,xml-encoding,Boost,Xml Parsing,Xml Encoding,我正在尝试使用Boost函数read\u-XML和write\u-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
<?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>
Lot1
123
试验
2.
项目1
项目第一测试
项目2
第二项测试
这就是输出:
<?xml version="1.0" encoding="utf-8"?>
<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>
Lot1
123
试验
2.
项目1
项目第一测试
项目2
第二项测试
这是我的C++代码(只是一个测试代码):
#包括
#包括
使用boost::property_tree::ptree;
树;
读取xml(文件xml,xmlTree);
for(auto it=xmlTreeChild.begin();it!=xmlTreeChild.end();)
{
std::string strItem=it->first.data();
如果(strcmp(strItem.c_str(),“项”)==0)
{
std::string strLabel=it->second.get_child(“标签”).data();
if(strcmp(strLabel.c_str(),“item3”)!=0)
{
它=xmlTreeChild.erase(它);
}
}
++它;
}
自动设置=boost::property\u tree::xml\u writer\u make\u设置('\t',1);
编写xml(文件xml,xmlTree,std::locale(),设置);
我需要使用与原始文件相同的编码来读取和重新写入文件。
我还尝试使用以下方法更改区域设置:
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);
std::locale newlocale1(“English_USA.1252”);
读取xml(文件xml,xmlTree,0,newlocale1);
...
自动设置=boost::property\u tree::xml\u writer\u make\u设置('\t',1);
写入xml(文件xml、xmlTree、newlocale1、设置);
但我得到了同样的结果
如何使用原始文件编码和Boost函数进行读写
谢谢您可以通过writer设置传递编码:
auto settings = boost::property_tree::xml_writer_make_settings<std::string>(
'\t', 1, "windows-1252");
auto settings=boost::property\u tree::xml\u writer\u make\u settings(
“\t”,1,“windows-1252”);
当然,确保键/值实际上与latin1/cp1252兼容(只要您从源文件读取所有信息,这是有意义的;但是,在将用户输入分配给属性树节点时,您必须小心;您可能需要首先将输入编码转换为cp1252)
您还可以按如下方式写入字符串流:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
boost::property_tree::ptree pt;
std::ostringstream oss;
write_xml(
oss, pt,
boost::property_tree::xml_writer_make_settings<char>(
'\t', 0, "ASCII"));
#包括
#包括
boost::property_tree::ptree pt;
std::ostringstream oss;
编写xml(
开放源码软件,
boost::property\u tree::xml\u writer\u make\u设置(
'\t',0,“ASCII”);
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
boost::property_tree::ptree pt;
std::ostringstream oss;
write_xml(
oss, pt,
boost::property_tree::xml_writer_make_settings<char>(
'\t', 0, "ASCII"));