Como iterar sobre estrutura XML em boost :: property_tree
Eu tenho uma estrutura XML ao longo das linhas de:
<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>
Que está sendo lido em umboost::property_tree
, Tem1. muitos <SomeElement>
s, e, em seguida, a uma profundidade arbitrária dentro desse elemento poderia haver1. muitos <ElementIWant>
s
Existe uma maneira de iterar sobre o<ElementIWant>
diretamente (em um único loop) na ordem em que aparecem no documento?
Eu olhei para o 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;
}
No entanto, parece não conseguir retornar nós (o que eu suspeito é porque o equal_range funciona no nível do nó da árvore fornecido), o que me leva à pergunta acima ...