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

How to read Boost property_map with peer values with identical tag names

本文关键字:对等 Boost map property 读取 何读取      更新时间:2023-10-16

我正在使用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"值节点呢?

在我的问题中的代码中,我要求的是树中过低的子节点。以下是将从"CS"节点检索"I"值的循环(替换我的问题中代码中的最后一个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...
}