c++ - 打印 boost 属性树生成的 xml

标签 c++ boost boost-propertytree

我测试了 boost::property_tree,结果很好:我可以加载 XML、提取元素、保存 XML 等。但是,是否可以生成 XML 并打印它?我不想保存它。

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

}

这是我用来加载和保存 XML 的函数。有什么方法可以显示吗?

最佳答案

使用以下版本的函数

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

控制台输出

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

stringstream中输出(可以在控制台输出stringstream内容,使用stringstreamstr函数).

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

关于c++ - 打印 boost 属性树生成的 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332035/

相关文章:

c++ - 使用 boost::property_tree 读取 ini 文件不适用于表单 A.x 的子项

c++ - 标准模板库列表 - 双向链接还是循环链接?

c++ - 无法调用复制构造函数

c++ - 如何在 Ubuntu 上的 Eclipse 中编译 C++11

gcc - 如何使用 GCC 强制构建 32 位 Boost?

c++ - 如何使用boost属性树使用boost解析json字符串中数组中的元素?

c++ - 任何人都可以提供有关 rgbx8888 格式的详细解释吗?

c++ - C++ 中的流类型,如何从 IstringStream 中读取?

c++ - 我可以在 boost::geometry 的多边形内存储附加信息吗?

c++ - 获取boost property_tree父节点