I'm trying to extract data from an XML that looks as shown in the image below using pugiXML traverse function. i'm able to print out all the data from the XML file using this function, however im not able to extract and store these into my variables. The main issue is that im not able to get node.name() and node.value() at the same time. node.name() comes in 1 iteration node.value() comes in the iteration after.
const char* node_types[] =
{
"null","Element", "Name", "InstancePath", "InstancePathDepth", "FieldAmount", "DataType", "TypeName", "DataSize", "ArrayTypeName", "LowerBound", "UpperBound", "BitPosition", "IsArray", "IsElementary", "IsProgram", "IsResource", "IsString", "IsStruct", "IsTask"
};
// tag::impl[]
struct simple_walker : pugi::xml_tree_walker
{
virtual bool for_each(pugi::xml_node& node)
{
for (int i = 0; i < depth(); ++i) std::cout << " "; // indentation
std::cout << node.name()<<" "<< node.value() << "\n";
return true; // continue traversal
}
};
The output is great, same as in the XML file.
You can skip elements that aren't of type node_element
and use node.child_value()
roughly like this:
virtual bool for_each(pugi::xml_node& node)
{
if (node.type() != pugi::node_element)
return true;
for (int i = 0; i < depth(); ++i) std::cout << " "; // indentation
std::cout << node.name()<<" "<< node.child_value() << "\n";
return true; // continue traversal
}
This is necessary because text (PCDATA) nodes are separate from element nodes: https://pugixml.org/docs/manual.html#node_pcdata