3

I'm following the Five Minute Tutorial and I get as output (unsurprisingly) the file debug_settings_out.xml.

But my problem is, that it isn't well formatted. It looks like this:

<?xml version="1.0" encoding="utf-8"?>
<debug><filename>debug.log</filename><level>2</level></debug>

and I want it to look like this:

<?xml version="1.0" encoding="utf-8"?>
<debug>
    <filename>debug.log</filename>
    <level>2</level>
</debug>

because it should be also manually editable. How can I achieve that?

I already found the settings I can pass to the parser, but none of them gave me the desired behaviour.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rico-E
  • 1,806
  • 2
  • 28
  • 47

1 Answers1

7

The documentation of PropertyTree is pretty bad (I've recently started improving it). What you need to do is pass a correct xml_writer_settings object to 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));
Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • Thank you. Can you explain to me, or show me where it is explained, what the `(' ', 4)` does? And what other options do I have? – Rico-E Jan 15 '14 at 13:31
  • 1
    The first argument is the indentation character, and the only other sensible choice is '\t' for a tab. The second is how often to repeat that character for each indent. There are no other options. – Sebastian Redl Jan 15 '14 at 13:35
  • 2
    In case anyone else ends up here, and cannot get it to work: when writing to a stream, instead of a file, there is no locale param. I.e. this works: `write_xml(std::cout, tree, xml_writer_make_settings(' ', 4));` – Darren Cook Apr 27 '14 at 06:07
  • any chance you could write the compiler / linker settings for use of this xml_writer_make_settings ?? I keep getting was not declared in this scope - even though I have use of ptree else where in my code – serup Sep 21 '16 at 10:14
  • If I don't pass a `` template argument to xml_writer_make_settings, then the compile fails: `xml_parser_writer_settings.hpp:55:30: note: candidate template ignored: couldn't infer template argument 'Str'` – qris Jan 22 '17 at 10:59
  • 1
    This is old, but in case anyone comes here looking for the solution, I found success by using the class created by xml_writer_make_settings directly, as write_xml(stream, tree, xml_parser::xml_writer_settings(' ', 4)); The template type specifier appears to be necessary. (As does property_tree scoping.) – Foster Boondoggle Mar 18 '21 at 16:05