如何使用具有相同标记名称的对等值读取Boost property_map

时间:2013-06-17 17:28:54

标签: c++ boost properties map boost-propertytree

我正在使用Boost c ++库v1.53中的property_map,它对我很有用,除了我无法弄清楚如何解析彼此相同名称的数据节点。如下面的XML:

<RECORDSET>
   <C>
      <CY>
          <CZ>
              <I>1</I>
              <CT>10</CT>
          </CZ>
          <CZ>
              <I>2</I>
              <CT>12</CT>
          </CZ>
      </CY>
      <CS>
          <I>1</I>
          <I>2</I>
      </CS>
   </C>
</RECORDSET>

除了底部“CS”标签下的“I”数据节点元素外,我可以解析上面的所有内容。我正在尝试使用代码:


   // (works no problem)
   BOOST_FOREACH(const ptree::value_type & vpairC, proptreeCs.get_child(string("C")))
   {
      if (vpairC.first.data() != std::string("C"))
         continue;

      // grab the "C" ptree sub-tree for readability.
      ptree proptreeC = vpairC.second;

      // (this works no problem to iterate through the multiple CZ nodes under CY)
      // RM_CZs 
      short nCZCount = 0;
      sTagName = ;
      BOOST_FOREACH(const ptree::value_type & vpairCZ, proptreeC.get_child("C"))
      {
         // get a local ptree for readability.
         ptree ptreeCZ = vpairCZ.second;

         // get the I and CT ids.
         sTagName = "I";
         long lId = ptreeCZ.get<long>(sTagName));
         sTagName = "CT";
         long lCT = ptreeCZ.get<long>(sTagName));

         // do something with id and ct...

         // increment the count.
         nCZCount++;
      }
      // nCZCount ends up set to 2 based on input XML above

      // (this loop does NOT work)
      sTagName = "CS";
      const ptree proptreeCS = proptreeC.get_child(sTagName);
      // (this does NOT work to iterate through <I> values under the <CS> node)
      sTagName = "I";
      BOOST_FOREACH(const ptree::value_type & vpairCS,
                    proptreeCS.get_child(sTagName))
      {
         // check to see if this is a "I" value; if not skip it.
         if (vpairCS.first.data() != sTagName)
            continue;
         long lId = atol(vpairCS.second.data().c_str());

         // do something with id...
      }
      // the above loop does NOT execute one time.
   }

那么我怎样才能遍历“CS”节点下的“I”值对等体?

1 个答案:

答案 0 :(得分:2)

在我的问题的代码中,我要求树中的孩子太低。这是一个将检索&#34; I&#34;来自&#34; CS&#34;的值node(替换我问题中代码中的最后一个BOOST_FOREACH):

BOOST_FOREACH(const ptree::value_type & vpairI, proptreeC.get_child(std::str("CS")))
{
   // check to see if this is an "I" value; if not skip it.
   if (vpairCapability.first.data() != std::string("I"))
      continue;

   long lId = atol(vpairI.second.data().c_str());

   // do something with lId...
}