如何在 boost::property_tree 中迭代 XML 结构

2023-12-30

我有一个 XML 结构:

<root>
 <SomeElement>
  <AnotherElement>
   <ElementIWant x="1" y="1"/>
  </AnotherElement>
 </SomeElement>
 <SomeElement>
  <AnotherElement>
   <ElementIWant x="1" y="1"/>
   <ElementIWant x="2" y="1"/>
   <ElementIWant x="3" y="1"/>
  </AnotherElement>
 </SomeElement>
</root>

哪个正在被读入boost::property_tree, 有1..Many <SomeElement>s,然后在该元素内的任意深度可能有1..Many <ElementIWant>s

有没有办法迭代<ElementIWant>直接(在单个循环中)按照它们在文档中出现的顺序?

我看过 equal_range

void iterateOverPoints()
{
     const char* test = 
     "<?xml version=\"1.0\" encoding=\"utf-8\"?><root>"
      "<SomeElement>"
       "<AnotherElement>"
        "<ElementIWant x=\"1\" y=\"1\"/>"
       "</AnotherElement>"
      "</SomeElement>"
      "<SomeElement>"
       "<AnotherElement>"
        "<ElementIWant x=\"1\" y=\"1\"/>"
        "<ElementIWant x=\"2\" y=\"1\"/>"
        "<ElementIWant x=\"3\" y=\"1\"/>"
       "</AnotherElement>"
      "</SomeElement>"
    "</root>";

    boost::property_tree::ptree message;
    std::istringstream toParse(test); 
    boost::property_tree::read_xml(toParse,result_tree);

    //Now we need to locate the point elements and set the x/y accordingly.
    std::pair< boost::property_tree::ptree::const_assoc_iterator,
               boost::property_tree::ptree::const_assoc_iterator > result =
         message.equal_range("ElementIWant");

    for( boost::property_tree::ptree::const_assoc_iterator it = result.first; 
           it != result.second; ++it )
    {
        std::cout  << it->first << " : ";
        const boost::property_tree::ptree& x = it->second.get_child( "<xmlattr>.x" );
        const boost::property_tree::ptree& y = it->second.get_child( "<xmlattr>.y" );
        std::cout << x.get_value<int>() << "," << y.get_value<int>() << "\n";
    }

    return;
}

然而,它似乎无法返回节点(我怀疑是因为 equal_range 在提供的树节点级别工作)这让我想到了上面的问题......


直接迭代所有元素是不可能的;文档说 http://www.boost.org/doc/libs/1_52_0/doc/html/boost_propertytree/container.html

没有办法迭代整个树。

现在,您可以使用递归,并在每个级别应用 STL 算法来模仿它;它不符合您在下面的示例中在单个循环中执行此操作的要求,但它确实有效:

template <typename InputIt, typename OutputIt, typename Compare>
void collect(InputIt first, InputIt last, OutputIt dest, Compare comp)
{
    typedef typename std::iterator_traits<InputIt>::reference reference;

    std::copy_if (
        first, last, dest,
        [comp] (reference what) { return comp(what.first); });

    std::for_each (
        first, last,
        [dest, comp] (reference what) { collect(what.second.begin(), what.second.end(), dest, comp); });
}


std::vector<std::pair<std::string, ptree>> match;

collect(
    xml.begin (), xml.end (), std::back_inserter(match),
    [] (const std::string& key) { return key == "ElementIWant"; });

for (auto pair: match)
{
     std::cout << pair.first << std::endl;
}

这是一个“完全”递归并保留出现顺序的版本:

template <typename InputIt, typename OutputIt, typename Compare>
void collect_recursive(InputIt first, InputIt last, OutputIt dest, Compare comp)
{
    typedef typename std::iterator_traits<InputIt>::reference reference;

    if (first == last)
    {
        return;
    }

    auto begin = first->second.begin ();
    auto end = first->second.end ();

    if (begin != end)
    {
        collect_recursive (begin, end, dest, comp);
    }

    if (comp (first->first))
    {
        dest = *first;
    }

    collect_recursive (++first, last, dest, comp);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 boost::property_tree 中迭代 XML 结构 的相关文章

随机推荐