我有一个XML文件,结构如下:
<Employee>
<Address>
<Name>XYZ</CustomerName>
<Street>street no. 1</Street>
<City>current city</City>
<Country>country</Country>
</Address>
</Employee>
我想提取节点Address
的所有节点的值,并希望将值存储在字符串向量中(即std::vector<std::string> EmployeeAdressDetails
)。
如何在循环中提取节点而不是逐个提取值?
更新:通过&#34;逐个提取&#34;,我的意思是以下内容:
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");
答案 0 :(得分:0)
你可以这样做:
for(auto node: doc.child("Employee").child("Address").children())
{
std::cout << node.name() << ": " << node.text().as_string() << '\n';
}
或者对于C++11
编译器:
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';
}
<强>输出:强>
Name: XYZ
Street: street no. 1
City: current city
Country: country