Reputation: 5649
I have an XML file with the following structure:
<Employee>
<Address>
<Name>XYZ</CustomerName>
<Street>street no. 1</Street>
<City>current city</City>
<Country>country</Country>
</Address>
</Employee>
I want to extract the values of all the nodes of the node Address
and want to store the values in a String Vector (i.e. std::vector<std::string> EmployeeAdressDetails
).
How can I extract the nodes in a loop instead of extracting values one by one?
UPDATE: By"Extracting one by one", I mean something like following:
xml_node root_node = doc.child("Employee");
xml_node Address_node = root_node.child("Address");
xml_node Name_node = Address_node .child("Name");
xml_node Street_node = Address_node .child("Street");
xml_node City_node = Address_node .child("City");
xml_node Country_node = Address_node .child("Country");
Upvotes: 0
Views: 2153
Reputation: 48605
You can do this:
for(auto node: doc.child("Employee").child("Address").children())
{
std::cout << node.name() << ": " << node.text().as_string() << '\n';
}
Or for pre C++11
compilers:
pugi::xml_object_range<pugi::xml_node_iterator> nodes = doc.child("Employee").child("Address").children();
for(pugi::xml_node_iterator node = nodes.begin(); node != nodes.end(); ++node)
{
std::cout << node->name() << ": " << node->text().as_string() << '\n';
}
Output:
Name: XYZ
Street: street no. 1
City: current city
Country: country
Upvotes: 1