Yea.. VARIANT is not part of the standard lib.
I got the feeling, that this forum here is great for answering questions about the standard libs and the standard language features in general (e.g. everything that starts with "std::" or where you include some file without any extension like "iostream" ;-)
But it's not optimal for specific third party libs. (For common and portable libs like from boost.org or Loki, you may get information here too).
A note about Denis' code: It should work just fine, but if you using exception handling in your program anyway, I would probably do it a bit differently:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
VARIANT GetAttribute(string attribute, IXMLDOMNamedNodeMap* pNodeMap)
{
if (pNodeMap == 0)
throw std::logic_error("called GetAttribute without an xml-element");
VARIANT varAtt;
IXMLDOMNode* pAtt = NULL;
_bstr_t tempb = attribute.c_str();
//Process varSource first to determine if to use WMI or TSVar
BSTR bstrAtt = tempb.copy();
pNodeMap->getNamedItem(bstrAtt, &pAtt);
if (pAtt)
{
pAtt->get_nodeValue(&varAtt);
SAFE_RELEASE(pAtt);
}
return varAtt;
}
|
It seems to me, that some user of GetAttribute should check for the existance of the whole XML element, before he tries to read an attribute for it. It's ok to try to read a non-existing attribute (which returns your VT_EMPTY. Quite sound interface). But any check to non-existing XML-elements seem not to belong into this function IMHO.
(Some prefer to replace the "if (pNodeMap..." lines with an "assert".)
But in the end, as I said: Denis' version does gets the job done too. :)
Ciao, Imi.