遍历Boost属性树 [英] iterate through boost property tree

查看:148
本文介绍了遍历Boost属性树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用boost属性
树遍历XML文档并将结果存储在结构中。我的问题是我只能将
转到第一个 item节点,而无法访问第二个 item
节点。我希望有人指出我犯了一个错误。

I'm iterating over an XML document using boost property tree and storing the results in a struct. The issue I have is that I can only get to the first "item" nodes and can't access the second "item" nodes. I was hoping someone would point out where I've made a mistake.

我的程序输出看起来像这样(您可以看到项目丢失了。那里
是没有显示的cookie2,candy2或Chocolate2项目):

My program output looks like this (you can see items are missing.. there is no cookie2, candy2 or chocolate2 items shown):

jar : snAcks
snack : coOkie
item : cooKie1
snack : canDy
item : caNdy1
snack : cHocolate
item : choColate1

这是xml文件:

<root>
    <jar name="snAcks">
        <snack name="coOkie">
           <item name="cooKie1"></item>
           <item name="cookIe2"></item>
        </snack>
        <snack name="canDy">
           <item name="caNdy1"></item>
           <item name="candY2"></item>
        </snack>
        <snack name="cHocolate">
           <item name="choColate1"></item>
           <item name="chocOlate2"></item>
        </snack>
    </jar>
</root>

这是源代码:

void parse_xml( boost::property_tree::iptree const& pt )
{
    BOOST_FOREACH( boost::property_tree::iptree::value_type const& v, pt.get_child("root.jar") )
    {
        // Show jar
        if ( boost::iequals( v.first, "<xmlattr>" ) )
        {
            std::cout << "jar : " << v.second.get<std::string>("NaME") << std::endl;
        }

        if ( boost::iequals( v.first, "snack" ) )
        {
            // Show snack
            std::cout << "snack : " << v.second.get<std::string>("<xmlattr>.name")  << std::endl;

            try
            {
                BOOST_FOREACH( boost::property_tree::iptree::value_type const& val, v.second.get_child("item") )
                {
                    if ( boost::iequals( val.first, "<xmlattr>" ) ) {
                        // Show item
                        std::cout << "item : " << val.second.get<std::string>("nAmE")  << std::endl;
                    }
                }
            }

            catch (boost::property_tree::ptree_bad_path& e)
            {
                // Show what caused exception
                std::cout << "Exception: " << e.what() << std::endl;
            }
        }
    }
}

谢谢您花时间阅读此书。我想我已经犯了一个简单的
错误,但无法理解在哪里。

Thank you for taking time to read this. I think I've made a simple mistake, but cannot understand where.

推荐答案

我知道了,但是

void parse_xml( boost::property_tree::iptree const& pt )
{
    BOOST_FOREACH(boost::property_tree::iptree::value_type const& v, pt.get_child("root.jar"))
    {
        // Show jar
        if ( boost::iequals( v.first, "<xmlattr>" ) ) {
            std::cout << "jar : " << v.second.get<std::string>("NaME") << std::endl;
        }

        if ( boost::iequals( v.first, "snack" ) )
        {
            BOOST_FOREACH(boost::property_tree::iptree::value_type const& val, v.second)
            {
                // Show snack
                if ( boost::iequals( val.first, "<xmlattr>" ) ) {
                    std::cout << "snack : " << val.second.get<std::string>("name")  << std::endl;
                }

                if ( boost::iequals(val.first, "item") )
                {
                    BOOST_FOREACH(boost::property_tree::iptree::value_type const& val2, val.second)
                    if ( boost::iequals( val2.first, "<xmlattr>" ) ) {
                        // Show item
                        std::cout << "item : " << val2.second.get<std::string>("nAmE")  << std::endl;
                    }
                }
            }
        }
    }
}

如果比较两个代码段,您会发现我的结构更加规则。

If you compare the two code snippets you can see that mine is bit more regularly structured.


  1. 遍历所有节点

  2. 处理< xmlattr> 节点

  3. 处理真实节点。

  4. 在该节点内从步骤1开始重复。

  1. Loop over all nodes
  2. process the <xmlattr> node
  3. process the "real" node.
  4. repeat from step 1. inside that node.

这篇关于遍历Boost属性树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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