Printing the xml generated by the boost property tree
Asked Answered
K

2

5

I tested boost::property_tree and it was good: I can load an XML, extract the elements, save the XML, etc. But, is it possible to generate an XML and print it? I don't 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 I use to load and save the XML. Do we have any method to display it?

Konstanz answered 11/3, 2013 at 5:51 Comment(2)
write in stream (std::stringstream) for example.Chamonix
@Chamonix Can you give me a small example?Konstanz
C
14

Use following version of 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 output in console

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

for output in stringstream (you can output stringstream contents in console, by using str function of stringstream).

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

Chamonix answered 11/3, 2013 at 6:4 Comment(1)
The link is deadKazim
I
0

You can use any kind of output stream, so std::cout will do just fine and print your XML to current console.

Innes answered 11/3, 2013 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.