0

I am using boost (version 1.70.0) property tree. If I have this XML (no empty lines):

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

and I extract a node, insert to another (empty) tree:

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();

I will get the output with empty lines, something like this:

  <Collection>



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

I have tried different things. I can fix it by iterating over Item elements and adding one by one. Then it works, no extra lines. However, if Item element itself has a child element(s), then again, it will add a bunch of empty lines.

M.K.
  • 159
  • 8

1 Answers1

1

I think it's duplicated with this one, or it's just a bug in the property tree.

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

To read with the trim flag will fix the problem:

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

To write with pretty format:

pt::write_xml(os, tree, boost::property_tree::xml_writer_make_settings<std::string>(' ', 1));
prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42