Xerces for C++: Processing an attribute of type double
Lets say I have an Xml file with the following element:
<average value=87.425 />
Im using Xerces for Cpp to parse this file, and now I want to process the attribute value that is of type double.
Is this the way to do it?
const XMLCh* pChAttrValue;
pChAttrValue = pAverageElem->getAttribute(XMLString::transcode("value"));
// Check if attribute value exists
if (pChAttrValue == XMLUni::fgZeroLenString) {
printf("Error: Average missing attribute \'value\'. \n");
return;
}
double* dValue;
char* chValue = XMLString::transcode(pChAttrValue);
sscanf(chValue, "%lf", &dValue);
XMLString::release(&chValue);
[703 byte] By [
Holly_vi] at [2007-11-19 4:02:40]

# 1 Re: Xerces for C++: Processing an attribute of type double
double* dValue;
char* chValue = XMLString::transcode(pChAttrValue);
sscanf(chValue, "%lf", &dValue);
XMLString::release(&chValue);
I am not sure about the sscanf part,
but I can assure you that this:
char* chValue = XMLString::transcode(pChAttrValue);
double dValue = atof(chValue);
XMLString::release(&chValue);
works for me (VC++ .net) atof should be widely spread, though.
And yes you should release chValue afterwards, if that is part of the question.
A thing that you might want to do is test wether you got a result or not:
double dValue;
char* chValue = XMLString::transcode(pChAttrValue);
if(chValue != NULL) {
dValue = atof(chValue);
XMLString::release(&chValue); //You might want to release at the bottom,
//but I am not sure. You gotta try.
} else {
dValue = -1; //Or whatever
}
XMLString::release(&chValue); // You might have to release here.
Hope this helps