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 

由于我没有启用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/。百度找找电影院什么的就好,搜代码实现是不靠谱。

时间: 2024-08-02 09:08:01

boost格式化输出xml的相关文章

XStream、JAXB 日期(Date)、数字(Number)格式化输出xml

XStream.Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目,利用它们将对象转换成xml时,经常会遇到日期(Date).数字按指定格式输出的需求,下面是使用示例: 一.日期字段格式化输出 1.1 xStream 1 XStream x = new XStream(); 2 x.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss", null,TimeZone.getTimeZone("GMT+

Boost 的C++ 格式化输出函式库:Format

他最大的特色是在于它可以使用C 语言中printf 的格式化字串,来针对C++ 的iostream 做输出.或是产生格式化的字串:相较于C++ iostream 的manipulator,boost::format 在使用上更为直觉.简单. 而且和printf 不同的地方在于,他又有C++ iostream 的type safe.可以支持自定义类型的输出 官方网站的介绍可以参考: http://www.boost.org/doc/libs/1_44_0/libs/format/index.htm

Java - 格式化输出JSON字符串的两种方式

目录 1 使用阿里的fastjson 1.1 项目的pom.xml依赖 1.2 Java示例代码 2 使用谷歌的gson 2.1 项目的pom.xml依赖 2.2 Java示例代码 1 使用阿里的fastjson 1.1 项目的pom.xml依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58&l

python变量及用户交互,用户名格式化输出

变量 name = wwjun Name2 = "wwjun" print ("name,Name2") 用户交互 Username = input ("username:") Password = input ("password:") Print(username,password) 用户名格式化输出 name = input ("name:") age = input ("age:"

var_dump() 格式化输出 | 显示不全

PHP 使用‘var_dump($var)’时 没有格式化输出,显得乱糟糟的.原因是没有启用‘XDebug’扩展 [php.ini] ; XDEBUG Extension zend_extension = "x:/wamp/bin/php/php5.5.12/zend_ext/php_xdebug-2.2.5-5.5-vc11.dll" var_dump()方法输出时候会展示不全出现省略号 解决下 在php.ini里的[xdebug]节点中,追加一下配置: xdebug.var_dis

python 3 用户输入和格式化输出

# -*- coding:utf-8 -*- #用户输入 && 格式化输出 #getpass 模块是内置的,可以将输入的内容隐藏 import getpass username = input("username:") password = input("password:") age = int (input("age:")) job = input("job:") #print(type(age),type

Python 格式化输出

2017-07-29  22:03:07 一.使用格式符%进行格式控制 %[(name)][flags][width].[precision]typecode (name):命名 flags:可以有+,-,' '或0.+表示右对齐.-表示左对齐.' '为一个空格,表示在正数的左侧填充一个空格,从而与负数对齐.0表示使用0填充 wideth : 表示显示宽度,占位个数 precision : 表示小数点后精度 typecode : 格式符为真实值预留位置,并控制显示的格式.格式符可以包含有一个类型

python开发基础:格式化输出

1 #format的三种玩法 2 res1 = '{},{},{}'.format('joker','male',18) 3 print(res1) #类似于占位符 4 res2 = '{1},{0},{1}'.format('joker','male',18) 5 print(res2) #类似于列表角标查找 6 res3 = '{name},{age},{sex}'.format(sex='male',name='joker',age='18') 7 print(res3) #指定参数 8

String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转)

String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转) //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf"; // Label1.Text = string.Format("asdfadsf{0:C}adsfas