PDA

View Full Version : Need help with QXmlStreamReader



jknotzke
25th October 2009, 14:03
Hi,

I am reading off a streaming socket. Which is to say, I can send commands to the server, but it generally constantly streams data in XML format.

The xml that is streamed isn't very well formatted. There's not much I can do about that. But, it tends to have this format:


<CinQoConnected id='1365p' />
<HardwareRevision id='1365p' timestamp='1256437880.14' version='10' />
<Manufacturer id='1365p' timestamp='1256437880.14' mfg_id='7' />
<ModelNumber id='1365p' timestamp='1256437880.14' model_id='1' />
<Power id='1365p' timestamp='1256437880.63' watts='0.00' />
<Torque id='1365p' timestamp='1256437880.63' Nm='0.00' />
<Cadence id='1365p' timestamp='1256437880.63' RPM='3.09' />
<Power id='1365p' timestamp='1256437910.59' watts='0.00' />
<Torque id='1365p' timestamp='1256437910.59' Nm='0.00' />
<Cadence id='1365p' timestamp='1256437910.59' RPM='8.39' />
<CinQoConnected id='1365p' />
<HardwareRevision id='1365p' timestamp='1256437917.34' version='10' />
<Manufacturer id='1365p' timestamp='1256437917.34' mfg_id='7' />
<ModelNumber id='1365p' timestamp='1256437917.34' model_id='1' />
<RawTorque id='1365p' timestamp='1256437921.09' Nm='-1' />
<OffsetTorque id='1365p' timestamp='1256437921.09' Nm='-98' />
<SensorConfiguration id='1365p' timestamp='1256437921.09' config='3' />
<BatteryVoltage id='1365p' timestamp='1256437921.58' voltage='2.96' />
<CinQoConnected id='1365p' />



This is the code I am using..


void QuarqdClient::readyForRead()
{

QXmlStreamReader xml(tcpSocket);
while(!xml.atEnd())
{
QXmlStreamReader::TokenType token = xml.readNext();
qDebug() << "Token is: " << token;
qDebug() << xml.name();
if(token == QXmlStreamReader::StartElement)
{
if(xml.name() == "Power")
{
QXmlStreamAttributes attrib = xml.attributes();
QStringRef watts = attrib.value("watts");
qDebug() << "Watts: " << watts;
}
else if(xml.name() == "Cadence")
{
QXmlStreamAttributes attrib = xml.attributes();
QStringRef rpm = attrib.value("RPM");
qDebug() << "Cadence: " << rpm;
}
}
}
if(xml.hasError()) {
qDebug() << xml.errorString();
}
xml.clear();
}


The problem is I am getting lots of xml errors. Unexpected end of document, Extra data in document.. etc, etc.

Not only that but I can't see to ever hit the cadence name.. I suspect it's hitting an error before..

Is this a good candidate for XmlStreamReader or am I better off reading the data and parsing it myself by hand ?

Oh and the stream of tokens looks like this:


Token is: 5
Token is: 1
Token is: 2
Token is: 4
Token is: 5
Token is: 3
Token is: 2
Token is: 4
Token is: 5
Token is: 3
Token is: 2
Token is: 4
Token is: 5
Token is: 3
Token is: 2
Token is: 4
Token is: 5
Token is: 1
Token is: 2
Token is: 4
Token is: 5
Token is: 3


Thanks

J

wysota
25th October 2009, 22:32
If you're streaming data you can't expect one while loop to do the job. At some point you will receive an error that you are out of data so you have to wait for more and continue reading. On the other hand your "xml data" is so trivial I wouldn't use an xml parser for it. Just read a line and parse it using QRegExp.

mgoetz
26th October 2009, 09:26
I agree with wysota. Your readyForRead() function is connected to the readyRead() of the QTcpSocket? Then using a synchronous while loop won't help. You have to create the QXmlStreamReader earlier.
Does the server you connect to create any xml root tag?