How to iterate PugiXML children using C++11 foreach-loop
PugiXML allows you to use the C++11 for loop (also known as range-based for loopor foreach loop) to iterate the children of a node easily:
<?xml version="1.0" encoding="UTF-8"?>
<root-element>
<sub>A</sub>
<sub>B</sub>
<sub>C</sub>
</root-element>
#include <iostream>
#include <pugixml.hpp>
using namespace std;
using namespace pugi;
int main() {
xml_document doc;
xml_parse_result result = doc.load_file("test.xml");
for(const auto& child : doc.child("root-element")) {
cout << child.child_value() << endl;
}
}
add_executable(pugixml-for pugixml-for.cpp)
target_link_libraries(pugixml-for pugixml)
Running the example will print
A
B
C