Boost属性树:如何使用xml文件获取子树的子树 [英] Boost property tree : how to get child of child tree with a xml file

查看:175
本文介绍了Boost属性树:如何使用xml文件获取子树的子树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Boost库(1.57.0.2版)解析一个简单的xml文件,并尝试访问链接字段值.

I try to parse a simple xml file using Boost library (version 1.57.0.2) and I try to access to link field values.

请在我的xml文件下面找到:

Please find, here below my xml file :

<?xml version="1.0" encoding="UTF-8"?>
<down>
    <food name="cream">
        <link name="123" />
        <link name="456" />
    </food>
    <tel name="ice">
        <link name="78" />
        <link name="90" />
    </tel>
</down>

我尝试查看如何从网络中获取孩子并做同样的事情,但是我不明白为什么它不能与我的代码一起使用.

I try to see how to get child of child from web and do the same but I don't understand why it doesn't work with my code.

我遇到以下错误:

error: 'const value_type {aka const struct std::pair<const std::__cxx11::basic_string<char>, boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >}' has no member named 'get_child'|

请在下面找到我的代码:

Please find my code here below :

boost::property_tree::xml_parser::read_xml(path.xml, tree);

BOOST_FOREACH(const boost::property_tree::ptree::value_type &child_tree, tree.get_child("down"))
{
    std::string tree_balise = tree.first;

    if (tree_balise == "food")
    {
        BOOST_FOREACH(const boost::property_tree::ptree::value_type &child_of_child_tree, child_tree.get_child("food"))
        {
            std::cout << "I am in the child of child tree" << std::endl;
        }
    }
}

有人可以告诉我为什么会出现此错误吗? 我想念什么吗?

Does somebody could tell me why this error appears ? Am I missing something ?

谢谢您的帮助.

Miwa.

推荐答案

  1. 实际上boost::property_tree::ptree::value_typekey_typepair(在您的情况下为std::string)和ptree.因此,在第二个FOREACH中,您必须使用child_tree.second,并且错误指出pair没有get_child成员函数.

  1. Actually boost::property_tree::ptree::value_type is a pair of key_type (std::string in your case) and ptree. So in 2nd FOREACH you have to use child_tree.second and error says that pair has no get_child member function.

在第二个FOREACH范围内,您已经在"food"子树中.因此它没有食物"孩子.

In 2nd FOREACH scope you are already in "food" subtree. So it doesn't have "food" child.

BOOST_FOREACH( const ptree::value_type & child_tree, tree.get_child("down") )
{
    std::string tree_balise = child_tree.first;

    if (tree_balise == "food")
    {
        BOOST_FOREACH( const ptree::value_type & child_of_child_tree, child_tree.second )
        {
            std::cout << "I am in the child of child tree " << child_of_child_tree.first << std::endl;
        }
    }
}

还请注意,您可以直接获取"down.food"子树.

Also Note that you can directly get "down.food" subtree.

BOOST_FOREACH( const ptree::value_type & child_tree, tree.get_child("down.food") )
{
    std::cout << "I am in the child of child tree " << child_tree.first << std::endl;
}

这篇关于Boost属性树:如何使用xml文件获取子树的子树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆