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('\t',1);
   write_xml(filename, pt,std::local(),settings);
}

意思是縮進1個\t,結果報了一大堆錯誤:

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 ('\t', 1);
  write_xml (filename, pt, std::locale (), settings);

To:

  auto settings = boost::property_tree::xml_writer_make_settings<std::string> ('\t', 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('\t', 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/。百度找找電影院什么的就好,搜代碼實現是不靠譜。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM