【问题标题】:boost property tree adds empty line with write_xml()boost 属性树用 write_xml() 添加空行
【发布时间】:2021-06-15 01:24:28
【问题描述】:

我正在使用 boost(版本 1.70.0)属性树。如果我有这个 XML(没有空行):

<Root>
  <SomeOtherElement>..</SomeOtherElement>
  <Collection>
     <Item Attr1=".." attr2="" />
     <Item Attr1=".." attr2="" />
  </Collection>
</Root>

然后我提取一个节点,插入另一个(空)树:

auto node = pt.get_child("Root.Collection");
ptree new_pt{};
new_pt.put_child("Collection", node);
std::ostringstream os;
write_xml(os, new_pt);
auto xml = os.str();

我会得到空行的输出,如下所示:

  <Collection>



     <Item Attr1=".." attr2="" />
     <Item Attr1=".." attr2="" />
  </Collection>

我尝试了不同的方法。我可以通过迭代 Item 元素并一一添加来修复它。然后它工作,没有额外的行。但是,如果 Item 元素本身有一个子元素,那么它又会添加一堆空行。

【问题讨论】:

  • 能否请您提供一个最小的可重现代码?
  • 上面的代码应该可以做到。您只需要添加包含,例如 boost/property_tree/xml_parser.hpp。抱歉,我之前有一个错字,现在更正了。

标签: c++ boost-propertytree


【解决方案1】:

我认为它与这个重复,或者它只是属性树中的一个错误。

https://stackoverflow.com/a/6614372/1292791

使用修剪标志读取将解决问题:

pt::read_xml(filename, tree, boost::property_tree::xml_parser::trim_whitespace);

用漂亮的格式写:

pt::write_xml(os, tree, boost::property_tree::xml_writer_make_settings<std::string>(' ', 1));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2012-12-07
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多