通过Pugixml C++提取XML文件中某个节点的所有节点
Extract all nodes of a node in an XML file by Pugixml C++
我有一个 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
)。
如何在循环中提取节点而不是一个一个地提取值?
更新: "Extracting one by one",我的意思是:
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");
你可以这样做:
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
我有一个 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
)。
如何在循环中提取节点而不是一个一个地提取值?
更新: "Extracting one by one",我的意思是:
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");
你可以这样做:
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