属性树:完整的路径和名称
Property tree: complete path and name
我想创建一个可以从属性树构造的类,如下例所示:
<?xml version="1.0" encoding="utf-8"?>
<config>
<name>testing</name>
<!-- Test property tree -->
<lambda min="200000" max="200">100</lambda>
...
对于属性树来说,这很容易,但接下来我需要访问子树的两个属性,就像在这个类中一样:
parameter::parameter(boost::property_tree::ptree t)
{
// Set the value
value = t.get_value<double>();
// ?????
auto nodename = t.something();
// ?????
std::string nodepath = t.somethingelse();
// Get the attributes (or empty)
auto p = t.get_child("<xmlattr>", boost::property_tree::ptree());
// If we have attributes, read them
if (p != boost::property_tree::ptree())
{
min = t.get<double>("<xmlattr>.min");
max = t.get<double>("<xmlattr>.max");
if (min > max)
throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath);
}
else
{
min = +1.0;
max = -1.0;
}
}
// ... Someplace else
lambda = parameter(config.get_child("config.lambda"));
在XML中,lambda
的mim/max属性无效,我需要抛出一个异常,可以读取为
Min and max values invalid for the parameter lambda, path: config.lambda
当然我可以直接传递字符串,但这会破坏目的。我试着搞乱t
的迭代器和data
,但一无所获。
可以从ptree
中获得这些值吗?
谢谢!
我会稍微打乱一下界面,这样你就不会过早地丢掉你需要的信息:
Live On Coliru
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
struct parameter {
parameter(ptree const& tree, ptree::path_type const& nodepath)
{
ptree const& t = tree.get_child(nodepath);
// Set the value
value = t.get_value<double>();
auto nodename = [nodepath] {
auto copy = nodepath;
while (!copy.single()) copy.reduce();
return copy.reduce();
}();
// Get the attributes (or empty)
auto p = t.get_child("<xmlattr>", boost::property_tree::ptree());
// If we have attributes, read them
if (p != boost::property_tree::ptree())
{
auto min = t.get<double>("<xmlattr>.min");
auto max = t.get<double>("<xmlattr>.max");
if (min > max)
throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath.dump());
}
else
{
min = +1.0;
max = -1.0;
}
}
private:
double min, max;
double value;
};
int main() {
ptree config;
std::ifstream xml("input.txt");
read_xml(xml, config);
auto lambda = parameter(config, "config.lambda");
}
打印
terminate called after throwing an instance of 'std::runtime_error'
what(): Min and max values invalid for the parameter lambda, path: config.lambda
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++概念:如何使用'concept'检查模板化结构的属性?
- C++A*算法并不总是在路径中具有目标节点
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- 从函数角度看ID到文件路径的内部与外部映射
- Qt Quick-如何仅从c++代码与qml属性交互
- CMake 错误 - 目标 foo INTERFACE_SOURCES属性包含在源目录中以前缀为前缀的路径
- 检查提升属性树中是否存在路径
- 如何使用Qt Windows中的设置路径属性从命令行运行应用程序(.exe)时在命令行上打印日志
- VS 2010 c++路径显示在项目属性中,但不显示在*.vcxprojf或*.sln文件中
- mErrCount:虽然在项目属性的include路径中声明了标识符,但未找到该标识符
- 属性树:完整的路径和名称
- 如果在目录路径的stat()失败时解释stat()属性,会发生什么?
- Boost属性树add_child指定路径