boost属性树xml编写器输出中没有行尾 [英] no end of line in boost property tree xml writer output

查看:106
本文介绍了boost属性树xml编写器输出中没有行尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用boost :: property_tree:

Consider the following code using boost::property_tree:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace boost::property_tree;

int main() {
    ptree model_tree;
    model_tree.add("calibrated", "true");
    model_tree.add("model.<xmlattr>.label", "label");
    model_tree.add("model.activity.<xmlattr>.type", "fixed");
    write_xml("test.xml", model_tree);
}

通过编译和执行程序,我得到以下输出:

By compiling and executing the program I get the following output:

<?xml version="1.0" encoding="utf-8"?>
<calibrated>true</calibrated><model label="label"><activity type="fixed"/></model>

这不是我真正期望的,因为没有换行和缩进。我想获取以下内容:

Which is not really what I expected, as there are no new lines nor indentation. I would like to get the following instead:

<?xml version="1.0" encoding="utf-8"?>
<calibrated>true</calibrated>
<model label="label">
    <activity type="fixed"/>
</model>

这是一个错误,还是可以选择获取后者的输出?

Is it a bug, or is there an option to obtain the latter output? Any help would be appreciated.

PS:我正在将Ubuntu 12.04 LTS与gcc 4.6.3和boost 1.48配合使用。

P.S.: I am using Ubuntu 12.04 LTS with gcc 4.6.3 and boost 1.48.

推荐答案

同时,我找到了答案。应该使用xml_writer_settings,即:

In the meantime I've found the answer. One should use xml_writer_settings, that is:

xml_writer_settings<char> settings(' ', 4);
write_xml("test.xml", model_tree, std::locale(), settings);

构造函数中的第一个参数是用于缩进的字符,而第二个参数是缩进长度。由于该解决方案尚无记录,因此希望这可以帮助其他面临类似问题的人。

The first parameter in the constructor is the character used for indentation, whilst the second is the indentation length. As the solution is undocumented, hopefully this will help others facing similar problems.

这篇关于boost属性树xml编写器输出中没有行尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆