When handling XML attributes in C++, what should be used to running different operations depending on the given attribute?

Currently, I have something like this:

// get list of attributes for an XML element into member called 'attributes'
// ...

// run appropriate functions for each attribute
for (auto attribute : attributes)
{
    auto name = attribute.name;
    auto value = attribute.value;

    if (name == "x")
        doSomethingWithX(value);
    else if (name == "y")
        doSomethingWithY(value);
}

For just a few attribute names, this isn't so bad - but with a larger number (>15) this starts to look messy and I'm concerned about performance issues.

Is there a better way for handling attributes like this?

1 Answers

1
πάντα ῥεῖ On

You can use a std::unordererd_map<std::string, std::function<void (const std::string&)>> and set it up with appropriate lambda functions:

std::unordererd_map<std::string, std::function<void (const std::string&)>> attrProcessors = {
    { "X", [](const std::string& value) {
           // Do something with value
           } } } ,
    { "Y", [](const std::string& value) {
           // Do something with value
           } } }
};

// run appropriate functions for each attribute
for (auto attribute : attributes)
{
    auto name = attribute.name;
    auto value = attribute.value;

    auto processorEntry = attrProcessors.find(name);

    if(processorEntry != attrProcessors.end()) {
       (*processorEntry).second(value);
    }
}

I am not so sure though that maintenace of the map entries would be easier to read than the if / else if cascade.
On the other hand you won't need to create an extra function for each attribute name.