boost :: property_tree xml漂亮的打印,格式化

Ric*_*o-E 3 c++ xml boost boost-propertytree

我正在遵循五分钟教程,我得到了输出(不出所料)的文件debug_settings_out.xml.

但我的问题是,格式不正确.它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<debug><filename>debug.log</filename><level>2</level></debug>
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<debug>
    <filename>debug.log</filename>
    <level>2</level>
</debug>
Run Code Online (Sandbox Code Playgroud)

因为它也应该是可手动编辑的.我怎样才能做到这一点?

我已经找到了可以传递给解析器的设置,但是它们都没有给我所需的行为.

Seb*_*edl 7

PropertyTree的文档非常糟糕(我最近开始改进它).您需要做的是将正确的xml_writer_settings对象传递给write_xml.

https://github.com/boostorg/property_tree/blob/master/include/boost/property_tree/detail/xml_parser_writer_settings.hpp

write_xml(filename, tree, std::locale(),
          xml_writer_make_settings(' ', 4));
Run Code Online (Sandbox Code Playgroud)

  • 如果其他人在这里结束,并且无法使其工作:当写入流而不是文件时,没有区域设置参数.即它的工作原理:`write_xml(std :: cout,tree,xml_writer_make_settings('',4));` (2认同)