I have a situation where my C++ program has to store ten different xml files of datasize of more than 1000 words each as a string and serve to requester based on the type of xml requested. I could think of storing strings in std::string variables and request can fall in if-else ladder which will return proper string based on the condition met. As an example -
string MyClass::getdata(char* xml)
{
string str1,str2,str3,str4;
str1.append("this is a sample xml one");
str2.append("this is a sample xml two");
str3.append("this is a sample xml thre");
str4.append("this is a sample xml four");
if(!strcmp(xml,"a.xml"))
return str1;
else if (!strcmp(xml,"b.xml"))
return str2;
else if (!strcmp(xml,"c.xml"))
return str3;
else if (!strcmp(xml,"d.xml"))
return str4;
else
{}
}
Although this solution would work but does not look like a professional programming and moreover not following C++ concepts also. Could you guys help me with some elegant/clean solution. It is important to note that all strings are constant only.
I'm not sure if pre-loading all XMLs, and storing them as std::string, is appropriate for you problem. But aside from that, your solution is fine. If you had 100+ XMLs, then an if-else ladder would be bad. But for 4 XMLs, it's OK.
More elegant/clean solution - use a container of key/value pairs (such as map or hash table) that will allow you to access XML file contents by filename. Instead of pre-loading all XMLs, load them once it is necessary (i.e. someone requests a file that is not yet in the container).