Как перебрать структуру XML в boost :: property_tree

У меня есть структура 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 работает на уровне поставляемого узла дерева), что подводит меня к вопросу выше …

7

Решение

Невозможно выполнить итерации по всем элементам напрямую; документация говорит

Нет способа перебрать все дерево.

Теперь вы можете использовать рекурсию и применять алгоритмы 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);
}
6

Другие решения

Других решений пока нет …