gpt4 book ai didi

c++ - 如何使用具有相同标签名称的对等值读取 Boost property_map

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:11 25 4
gpt4 key购买 nike

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

最佳答案

在我问题的代码中,我要求 child 在树上太低。这是将从“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...
}

关于c++ - 如何使用具有相同标签名称的对等值读取 Boost property_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17153350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com