c++xml-parsingpugixml

pugixml - iterating over specific nodes


I have a xml document with a node which has children nodes but I want to iterate over specific nodes which names are stored in the array, for example:

const char* childrenNodes[]={"childNodeA", "childNodeC", "childNodeK"};

I can use next_sibling function which takes as an argument the element of the above array. Do you have any idea how to implement such loop using pugixml ?


Solution

  • There are two overrides of next_sibling

    xml_node next_sibling() const;
    xml_node next_sibling(const char_t* name) const;
    

    You're concentrating on the second one. Rather use the first one that doesn't take a parameter, and then just check if the node name is in the array

    pugi::xml_node node = root.first_child();
    for (; node; node = node.next_sibling()
    {
        if (std::find(std::begin(childrenNodes), std::end(childrenNodes), node.name()) != std::end(childrenNodes))
        {
            // ...
        }
    }