Printing xml generated by boost property tree

I tested boost::property_tree, and that was good: I can load XML, retrieve elements, save XML, etc. But is it possible to generate XML and print it? I do not want to save it.

void debug_settings::load(const std::string &filename) {
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(filename, pt);
    m_file = pt.get<std::string>("debug.filename");
    m_level = pt.get("debug.level", 0);
    BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.modules"))m_modules.insert(v.second.data());

}

void debug_settings::save(const std::string &filename) {
    using boost::property_tree::ptree;
    ptree pt;
    pt.put("debug.filename", m_file);
    pt.put("debug.level", m_level);
    BOOST_FOREACH(const std::string &name, m_modules)pt.add("debug.modules.module", name);
    write_xml(filename, pt);

}

This is the function that I use to load and save XML. Do we have any way to display it?

+5
source share
2 answers

Use the next version of the function

template<typename Ptree> 
  void write_xml
  (
     std::basic_ostream< typename Ptree::key_type::value_type > & stream, 
     const Ptree & pt, 
     const xml_writer_settings< typename Ptree::key_type::value_type > & settings = 
     xml_writer_settings< typename Ptree::key_type::value_type >()
  );

http://www.boost.org/doc/libs/1_52_0/doc/html/boost/property_tree/xml_parser/write_xml_id1233444.html

write_xml(std::cout, pt);

for console output

std::ostringstream oss;
write_xml(oss, pt);

to output to stringstream(you can display the contents stringstreamin the console using the strfunction stringstream).

http://liveworkspace.org/code/4qV9om $ 4

+13

, std:: cout XML .

0

All Articles