boost格式化输出xml

  我的boost为1.56而不是1.55

  boost在xml的例子给出了一段写xml文件的代码,我简化如下:

void debug_settings::save(const std::string &filename)
{
   using boost::property_tree::ptree;
   ptree pt;

   pt.put("debug.filename", m_file);
   pt.put("debug.level", m_level);

   write_xml(filename, pt);
}

这段代码写出来的格式非常难看,没有缩进的。于是在网上查找了下,需要改成下面的样子:

void debug_settings::save(const std::string &filename)
{
   using boost::property_tree::ptree;
   ptree pt;

   pt.put("debug.filename", m_file);
   pt.put("debug.level", m_level);

   boost::property_tree::xml_writer_settings<char> settings('	',1);
   write_xml(filename, pt,std::local(),settings);
}

意思是缩进1个 ,结果报了一大堆错误:

In instantiation of 'class boost::property_tree::xml_parser::xml_writer_settings<char>':
../../game_server/common/CServerSetting.cpp:97:61:   required from here
../../game_server/libraries/boost_1_56_0/boost/property_tree/detail/xml_parser_writer_settings.hpp:38:35: error: 'char' is not a class, struct, or union type
  typedef typename Str::value_type Ch;

本人水平在菜,没见过此类错误。去百度,也找不到类似的结果。看xml_parser_writer_settings的源代码,也看不懂。白白浪费了几个小时,终于受不了,翻出去请教google,很快找到了相关的解决方案:

http://www.pcl-users.org/PCL-compilation-errors-Please-help-me-td4035209.html

This is because there is a breaking API change in the boost 1.56.0 property_tree, as compared to boost 1.55.0. For more reference, see an issue described here: link.

I fixed this by modifying:

  boost::property_tree::xml_writer_settings<char> settings ('	', 1);
  write_xml (filename, pt, std::locale (), settings);

To:

  auto settings = boost::property_tree::xml_writer_make_settings<std::string> ('	', 1);
  write_xml (filename, pt, std::locale (), settings);

In the 3 or so places this occurs...

Thanks,

McDamon 

http://lists.boost.org/boost-users/2014/08/82693.php

 Dear all,

with the release of Boost 1.56, on Ubuntu 14.04 (g++ 4.8.2, 64 bit),
code like the following suddenly fails to compile:

pt::xml_writer_settings<char> settings('	', 1);
pt::write_xml(someFileName, ptr_out, std::locale(), settings);

"pt" is obviously an alias for boost::property_tree. The error message
I'm getting is

/opt/boost156/include/boost/property_tree/detail/xml_parser_writer_settings.hpp:38:35:
error: 'char' is not a class, struct, or union type
  typedef typename Str::value_type Ch;

I can see the following possibly relevant change in property_tree:

In Boost 1.55, from xml_parser.hpp:
-----------------------------------

template<typename Ptree>
void write_xml(
  const std::string &
  , const Ptree &
  , const std::locale & = std::locale()
  , const xml_writer_settings<typename Ptree::key_type::value_type >& =
xml_writer_settings<typename Ptree::key_type::value_type >()
);

In Boost 1.56, same header:
---------------------------

template<typename Ptree>
void write_xml(
  const std::string &
  , const Ptree &
  , const std::locale & = std::locale()
  , const xml_writer_settings<typename Ptree::key_type > &
= xml_writer_settings<typename Ptree::key_type >()
);

So xml_writer_settings is now given a Ptree::key_type instead of a
Ptree::key_type::value_type which I assume is the reason for the above
error.

Is there a portable way to specify the type of indention character in
write_xml ?

Best Regards,
Beet 
View Code

由于我没有启用C++11,改为这样写

    boost::property_tree::xml_writer_settings<string> settings =
            boost::property_tree::xml_writer_make_settings<string> (' ', 4);
    write_xml( DEFAULTCONFIG,pt,std::locale(),settings );

问题解决。

  PS:大伙以后搜索代码用这个http://www.gfsoso.com/。百度找找电影院什么的就好,搜代码实现是不靠谱。

原文地址:https://www.cnblogs.com/coding-my-life/p/4070201.html