How to iterate over XML structure in boost::property_tree

你说的曾经没有我的故事 提交于 2019-12-03 12:43:58

It is not possible to iterate over all elements directly; the documentation says

There is no way to iterate over the entire tree.

Now, you could use recursion, and apply STL algorithms at each level to mimic that; it does not fit your requirement of doing this in a single loop in my sample below, but it does works:

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;
}

Here is a version that is "fully" recursive and preserve the order of appearance:

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!