11

In order to pretty print my XML output with boost::property_tree, I wrote the following code:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main()
{
    std::string filename = "test.xml";
    boost::property_tree::ptree pt;
    pt.put("some.path.value", "hello");

    boost::property_tree::xml_writer_settings<char> settings('\t', 1);
    write_xml(filename, pt, settings);
}

Unfortunately I have this error and I can't find any information about it:

/usr/local/include/boost/property_tree/detail/xml_parser_writer_settings.hpp:38:19: error: type 'char' cannot be used prior to '::' because it has no members
    typedef typename Str::value_type Ch;
                     ^

Any idea?

sehe
  • 374,641
  • 47
  • 450
  • 633
Martin Delille
  • 11,360
  • 15
  • 65
  • 132

1 Answers1

13

I'd use the helper function

std::ofstream file("test.xml");

boost::property_tree::ptree pt;    
pt.put("some.value", "test");

boost::property_tree::write_xml(
   file, pt,
   boost::property_tree::xml_writer_make_settings<std::string>('\t', 1));
sehe
  • 374,641
  • 47
  • 450
  • 633
  • It works but how can I use it with write_xml? When writing `write_xml(filename, pt, boost::property_tree::xml_writer_make_settings('\t', 1))` I have the following error: `no matching function for call to 'write_xml` – Martin Delille Mar 31 '15 at 14:48
  • 1
    @MartinDelille You need to pass a stream, not a filename. The documentation is patient :) http://www.boost.org/doc/libs/1_57_0/doc/html/boost/property_tree/xml_parser/write_xml_idp113303904.html (also funny you truncated the message because now I'm having to guess what the types of `filename` and `pt` are). – sehe Mar 31 '15 at 14:48
  • There is an issue with changing from ` to ` it only works if you are building on a 64bit, otherwise if you are building on 32bit and try to use then you will get above described error, however if you are building on 64bit you will not get the error -- this assuming that you used a stream and not filename – serup Dec 30 '16 at 13:41
  • @serup that makes little sense. It seems like you may be conflating character encoding and build architecture. – sehe Dec 30 '16 at 14:09
  • @sehe, I agree it makes little sense, none the less it is the fact - try it out – serup Jan 02 '17 at 09:12