c++ - 如何使用 boost 解析 XML 中的 2 个以上级别

标签 c++ xml parsing boot

我从 boost 库中获得了这段代码。 http://www.boost.org/doc/libs/1_42_0/doc/html/boost_propertytree/tutorial.html

这是他们的xml文件

<debug>
    <filename>debug.log</filename>
    <modules>
        <module>Finance</module>
        <module>Admin</module>
        <module>HR</module>
    </modules>
    <level>2</level>
</debug>

加载这些值并打印它们的代码是

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <set>
#include <exception>
#include <iostream>

struct debug_settings
{
    std::string m_file;               // log filename
    int m_level;                      // debug level
    std::set<std::string> m_modules;  // modules where logging is enabled
    void load(const std::string &filename);
    void save(const std::string &filename);
};

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.put("debug.modules.module", name,true);

    write_xml(filename, pt);
}

int main()
{
    try
    {
        debug_settings ds;
        ds.load("debug_settings.xml");
        ds.save("debug_settings_out.xml");
        std::cout << "Success\n";
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << "\n";
    }
    return 0;
}

但它给了我一个错误

/usr/include/boost/property_tree/detail/ptree_implementation.hpp:769: error: request for member ‘put_value’ in ‘tr’, which is of non-class type ‘bool’

谁能告诉我我错过了什么?

最佳答案

似乎他们已经替换了 put () 函数...所以如果我更改行

"pt.put("debug.modules.module", name,true);"

"pt.add("debug.modules.module", name);"

它工作正常。谢谢。

关于c++ - 如何使用 boost 解析 XML 中的 2 个以上级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12698938/

相关文章:

c++ - ActiveMQCPP connection.start() 挂了

java - XML 解析 - 从特定节点解析文本时出现问题

python - Errno 2 使用 python shutil.py 文件目标没有这样的文件或目录

c# - 使用 XmlTextWriter 设置多个命名空间

java - 无法解析包含特殊字符的值?使用 sax 解析器

parsing - Haskell Parsec - 使用自定义 token 时错误消息的帮助不大

c++ - 如何从数组中消除重复数字,并将数组大小调整为 C++ 中的新元素数

c++ - 将 lambda 传递给数值库

c - 使用 strtok 解析 mmaped 文件?

c++ - 有没有办法在不手动查找的情况下获取数组的大小?