I’m reading from an xml file and trying to parse the data to a double. It is reading the data and displays the appropriate string in the application output but wont convert from string to double. This is the section of code doing the conversion.
if ( !(imgDur = QString ( xmlReader->readElementText() ).toDouble()) ){
imgDur = 10;
}
this returns the number 0. I get zero errors and the code compiles. Why wont this work? Thank you for you time.
The entire loop that reads the XML file
//Parse the XML until we reach end of it
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
// Read next element
QXmlStreamReader::TokenType token = xmlReader->readNext();
//If token is just StartDocument - go to next
if(token == QXmlStreamReader::StartDocument) {
continue;
}
//If token is StartElement - read it
if(token == QXmlStreamReader::StartElement) {
if(xmlReader->name() == "time_delay") {
qWarning() << xmlReader->readElementText(); // OUTPUTS 15
if ( !(imgDur = QString (xmlReader->readElementText()).toDouble() ) ){
qWarning() << "AS AN DOUBLE" << imgDur; // OUTPUTS 0
imgDur = 10;
}
}
if(xmlReader->name() == "value") {
qWarning() << xmlReader->readElementText(); // OUTPUTS 8
}
}
}
The main problem of your code was this
qWarning() << xmlReader->readElementText(); // OUTPUTS 15line.QXmlReader::readElementText()moves xmlReader pointer to the end of the text node, so thereadNext()will returnEndElementofAinstead ofQXmlStreamReader::Characterstoken. So basiclyreadElementTextdoes something like that (note that in the real implementation it is a lot more complicated, as it checks default behaviour, sets QXmlStreamReader internal data/status/token etc):So basicly second
xmlReader->readElementText();will always return empty QString, as the current token is notQXmlStreamReader::StartElementanymore