Ho una struttura XML lungo le linee di:Come iterare su struttura XML in boost :: property_tree
<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>
che viene letto in un boost::property_tree
, ci sono 1..Many<SomeElement>
s, e poi ad una profondità arbitraria all'interno di tale elemento potrebbe esserci 1..Many<ElementIWant>
s
c'è un modo per scorrere il <ElementIWant>
direttamente (in un singolo ciclo) nell'ordine che appea r nel doc?
Ho guardato 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;
}
Tuttavia sembra non riuscire a tornare nodi (che ho il sospetto è perché equal_range funziona a livello del nodo della struttura in dotazione) che mi porta alla domanda di cui sopra. ..
Hai provato 'equal_range (" SomeElement.AnotherElement.ElementIWant ");'? Non sono sicuro di cosa farà quando ci sono due copie di SomeElement, però. –
Esplosione dal passato! @TreborRude Penso che al momento non si comportasse come volevo, o non ha funzionato. Forse la libreria è stata migliorata dal momento che per farlo - ma io sono ben lontano da quell'area di codice al momento. :) Grazie per il suggerimento però. – Caribou