【问题标题】:Boost property tree Bad path for nothing提升属性树 无用的坏路径
【发布时间】:2015-01-05 05:41:41
【问题描述】:

我在使用这个库时遇到了问题...我的代码工作正常,解析器/创建器也工作,但出现错误,我不知道为什么:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/thread.hpp>
#include <string>
#include <exception>
#include <set>
#include <iostream>
#include "journal.h"

unsigned short port                 = 2013;
unsigned short maxConnec            = 250;

unsigned short fPort()                 {return port;}
unsigned short fMaxConnec()            {return maxConnec;}

bool load(const std::string &file)
{
    using boost::property_tree::ptree;
    ptree objectXML;
    std::cout << "bbb";
    read_xml(file, objectXML);
    std::cout << "aaa";
    if (file.length() == 0) // By the way, no way to do that better ? "if file doesn't exist..."
    {
        return 0;
    }
    else
    {
        port                    = objectXML.get<unsigned short>("configuration.server.port");
        maxConnec            = objectXML.get<unsigned short>("configuration.server.maxConnections");
        return 1;
    }
}

bool save(const std::string &file)
{
    try
    {
        using boost::property_tree::ptree;
        ptree objectXML;
        objetXML.put("configuration.server.port", port);
        objetXML.put("configuration.server.maxConnections", maxConnec);
        write_xml(file, objectXML, std::locale(), boost::property_tree::xml_writer_make_settings<ptree::key_type>(' ', 4));
        return 1;
    }
    catch (std::exception e)
    {
        return 0;
    }
}

void generate()
{

    std::string file = "configuration.xml";
    try{
        if (!load(fichier))
        {
            save(file);
        }
    }
    catch (std::exception &e)
    {
        load(file);
    }
}

获得一条错误的路径,我完全不知道为什么,因为当我尝试读取数据时,我可以并且即使我更改它,它也会在configuration.xml 中获取数据......

【问题讨论】:

    标签: c++ exception boost boost-propertytree


    【解决方案1】:

    ptree_bad_path 异常是从 get 的抛出版本引发的,并表示 XML 元素"configuration.server.port""configuration.server.maxConnections" 路径不存在。

    该错误与configuration.xml 文件路径无关。

    所以你应该检查元素名称,或者对于可选元素,使用default-value / optional-value version of get

    【讨论】:

    • 嗯,这正是我刚刚按下“发布”按钮时的想法,我感觉很傻,因为我必须走了。但无论如何,就是这样,谢谢。其实我删了一些东西贴出来,让代码更易读,就是configuration.time.save(我找的是configuration.time.saving)。无论如何,再次感谢你:)。
    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多