Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call different processing functions for attributes in an XML element

Tags:

c++

xml

When handling XML attributes in C++, how should different operations be run for different attributes?

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.

What might be a better way of handling XML attributes like this?

like image 413
ImJimmi Avatar asked Apr 22 '19 08:04

ImJimmi


People also ask

What is the function of each element and attribute in your XML file?

The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element. In the above syntax element_name is the name of an element which can be any name.

What are different types of elements and attributes in XML?

ID − It is used to specify the element as unique. IDREF − It is used to reference an ID that has been named for another element. IDREFS − It is used to reference all IDs of an element. ENTITY − It indicates that the attribute will represent an external entity in the document.

Can XML attributes have multiple values?

attributes cannot contain multiple values (elements can)

What is an XML attribute?

The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element. In the above syntax element_name is the name of an element which can be any name. The attribute1, attribute2, … is XML attribute having unique attribute name.

How do you use elements in XML?

Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data. Rules always have exceptions. Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML.

Can I use single quotes for attributes in XML?

If the attribute value itself contains double quotes you can use single quotes, like in this example: In the first example, gender is an attribute. In the last example, gender is an element. Both examples provide the same information. There are no rules about when to use attributes or when to use elements in XML.

What is notation type in XML?

Notation Type: This attribute is used to declares that an element will be referenced to a notation which is declared somewhere else in the XML document. Enumeration: This attribute is used to specify a particular list of values which match with attribute values. ID: This attribute is used to identify the element.


1 Answers

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.

like image 89
πάντα ῥεῖ Avatar answered Oct 15 '22 14:10

πάντα ῥεῖ