Reputation: 8003
I have this question about the boost xml parsing:
here is a piece of my Xml:
<Clients>
<Client name="Alfred" />
<Client name="Thomas" />
<Client name="Mark" />
</Clients>
and I read the name with this code:
std::string name = pt.get<std::string>("Clients.Client.<xmlattr>.name, "No name");
and works fine, but retrieve always the first node..
Is there a way to get the second, third node without looping?
thanks
Upvotes: 3
Views: 4632
Reputation: 392931
There's no facility to query multi-valued keys in Property Tree. (Partly because most of the supported backend formats do not officially support duplicate keys).
However, you can iterate through child elements, so you can implement your own query, like so:
for (auto& child : pt.get_child("Clients"))
if (child.first == "Client")
std::cout << child.second.get<std::string>("<xmlattr>.name", "No name") << "\n";
See fully working sample Live On Coliru:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>
#include <iostream>
using boost::property_tree::ptree;
int main()
{
std::stringstream ss("<Clients>\n"
" <Client name=\"Alfred\" />\n"
" <Client name=\"Thomas\" />\n"
" <Client name=\"Mark\" />\n"
"</Clients>");
ptree pt;
boost::property_tree::read_xml(ss, pt);
for (auto& child : pt.get_child("Clients"))
{
if (child.first == "Client")
std::cout << child.second.get<std::string>("<xmlattr>.name", "No name") << "\n";
}
};
Upvotes: 5