How to loop into nodes with same name

时间:2017-07-10 15:23:52

标签: c++ boost tree

I have a part of an XML with the following data :

           <DATA>
              <VALUE>July</VALUE>
              <VALUE>August</VALUE>
           </DATA>

In the following code, "kv.first()" is equal to "DATA"

BOOST_FOREACH(const boost::property_tree::ptree::value_type &kv, children)
{
    std::cout << "kv.first : " << kv.first << std::endl;
    std::cout << "kv.second.data : " << kv.second.data() << std::endl;
}

And what I see with the std::cout is :

kv.first : DATA
kv.second.data : July

What I expect is rather :

kv.first : DATA
kv.second.data : July
kv.first : DATA
kv.second.data : August

Did I write something wrong ? Or something is missing ?

Thank you for your answers.

1 个答案:

答案 0 :(得分:1)

您需要修改BOOST :: FOREACH循环以包含根标记:

BOOST_FOREACH(ptree::value_type &v, pt.get_child("DATA"))

pt ptree 如果您的根标记之前有一些值,则还需要包含它们,例如:

 <ROOT>
   <DATA>
      <VALUE>July</VALUE>
      <VALUE>August</VALUE>
   </DATA>
 </ROOT>

现在,foreach循环变为:

BOOST_FOREACH(ptree::value_type &v, pt.get_child("ROOT.DATA"))