2

I am using the property_map from Boost c++ library v1.53, and its working great for me except I can't figure out how to parse data nodes with the same name that are peers of each other. As in the following 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>

I can parse everything above except the "I" data node elements under the "CS" tag at the bottom. I am trying to use the code:


   // (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.
   }

So how can I iterate through the "I" value peers under the "CS" node?

llonesmiz
  • 155
  • 2
  • 11
  • 20
ATLdev
  • 81
  • 1
  • 4

1 Answers1

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...
}
ATLdev
  • 81
  • 1
  • 4