2

我正在使用来自 Boost c++ library 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”值对等点呢?

4

1 回答 1

2

In the code in my question, I was asking for children too low in the tree. Here is the loop that will retrieve the "I" values from the "CS" node (replaces the last BOOST_FOREACH in the code in my question):

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...
}
于 2013-06-18T13:11:34.203 回答