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?
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.
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.
attributes cannot contain multiple values (elements can)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With