Mam struktury XML wzdłuż linii:Jak iteracyjne nad strukturę XML w 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>
które są odczytywane w boost::property_tree
, Są 1..Many<SomeElement>
s, a następnie w dowolnej głębokości w którym element nie może być 1..Many<ElementIWant>
s
Czy iterację na <ElementIWant>
bezpośrednio (w jednej pętli) w kolejności, w jakiej appea r w dokumencie?
Mam spojrzał na 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;
}
Jednak wydaje się, że nie zwróci węzłów (co podejrzewam dlatego equal_range działa na poziomie węzła drzewa dostarczonego), który doprowadza mnie do pytania powyżej. ..
Czy wypróbowałeś 'equal_range (" SomeElement.AnotherElement.ElementIWant ");'? Nie jestem pewien, co się stanie, gdy pojawią się dwie kopie SomeElement. –
Wybuch z przeszłości! @TreborRude Myślę, że w tym czasie nie zachowywało się tak, jak chciałem, lub nie działało. Być może biblioteka została ulepszona, ponieważ to robię - ale w tej chwili jestem daleko od tego obszaru kodu. :) Dzięki za sugestię. – Caribou