【发布时间】: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。抱歉,我之前有一个错字,现在更正了。