iterate through boost property tree

后端 未结 1 391
無奈伤痛
無奈伤痛 2021-02-06 15:13

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

1条回答
  •  [愿得一人]
    2021-02-06 15:41

    I figured it out, but this is not what i would call an intuitive xml-parser library.

    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, "" ) ) {
                std::cout << "jar : " << v.second.get("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, "" ) ) {
                        std::cout << "snack : " << val.second.get("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, "" ) ) {
                            // Show item
                            std::cout << "item : " << val2.second.get("nAmE")  << std::endl;
                        }
                    }
                }
            }
        }
    }
    

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

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

    0 讨论(0)
提交回复
热议问题