0

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
        }
    };

enter image description hereenter image description here

The output is great, same as in the XML file.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this was very helpful.
Now how to call for_each struct ? make instance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.