Results 1 to 3 of 3

Thread: Need help with QXmlStreamReader

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Need help with QXmlStreamReader

    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:

    Qt Code:
    1. <CinQoConnected id='1365p' />
    2. <HardwareRevision id='1365p' timestamp='1256437880.14' version='10' />
    3. <Manufacturer id='1365p' timestamp='1256437880.14' mfg_id='7' />
    4. <ModelNumber id='1365p' timestamp='1256437880.14' model_id='1' />
    5. <Power id='1365p' timestamp='1256437880.63' watts='0.00' />
    6. <Torque id='1365p' timestamp='1256437880.63' Nm='0.00' />
    7. <Cadence id='1365p' timestamp='1256437880.63' RPM='3.09' />
    8. <Power id='1365p' timestamp='1256437910.59' watts='0.00' />
    9. <Torque id='1365p' timestamp='1256437910.59' Nm='0.00' />
    10. <Cadence id='1365p' timestamp='1256437910.59' RPM='8.39' />
    11. <CinQoConnected id='1365p' />
    12. <HardwareRevision id='1365p' timestamp='1256437917.34' version='10' />
    13. <Manufacturer id='1365p' timestamp='1256437917.34' mfg_id='7' />
    14. <ModelNumber id='1365p' timestamp='1256437917.34' model_id='1' />
    15. <RawTorque id='1365p' timestamp='1256437921.09' Nm='-1' />
    16. <OffsetTorque id='1365p' timestamp='1256437921.09' Nm='-98' />
    17. <SensorConfiguration id='1365p' timestamp='1256437921.09' config='3' />
    18. <BatteryVoltage id='1365p' timestamp='1256437921.58' voltage='2.96' />
    19. <CinQoConnected id='1365p' />
    To copy to clipboard, switch view to plain text mode 

    This is the code I am using..

    Qt Code:
    1. void QuarqdClient::readyForRead()
    2. {
    3.  
    4. QXmlStreamReader xml(tcpSocket);
    5. while(!xml.atEnd())
    6. {
    7. QXmlStreamReader::TokenType token = xml.readNext();
    8. qDebug() << "Token is: " << token;
    9. qDebug() << xml.name();
    10. if(token == QXmlStreamReader::StartElement)
    11. {
    12. if(xml.name() == "Power")
    13. {
    14. QXmlStreamAttributes attrib = xml.attributes();
    15. QStringRef watts = attrib.value("watts");
    16. qDebug() << "Watts: " << watts;
    17. }
    18. else if(xml.name() == "Cadence")
    19. {
    20. QXmlStreamAttributes attrib = xml.attributes();
    21. QStringRef rpm = attrib.value("RPM");
    22. qDebug() << "Cadence: " << rpm;
    23. }
    24. }
    25. }
    26. if(xml.hasError()) {
    27. qDebug() << xml.errorString();
    28. }
    29. xml.clear();
    30. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. Token is: 5
    2. Token is: 1
    3. Token is: 2
    4. Token is: 4
    5. Token is: 5
    6. Token is: 3
    7. Token is: 2
    8. Token is: 4
    9. Token is: 5
    10. Token is: 3
    11. Token is: 2
    12. Token is: 4
    13. Token is: 5
    14. Token is: 3
    15. Token is: 2
    16. Token is: 4
    17. Token is: 5
    18. Token is: 1
    19. Token is: 2
    20. Token is: 4
    21. Token is: 5
    22. Token is: 3
    To copy to clipboard, switch view to plain text mode 

    Thanks

    J

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help with QXmlStreamReader

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    37
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Need help with QXmlStreamReader

    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?
    Disclaimer: Although I work on Qt for Nokia, anything I post here is personal

Similar Threads

  1. QXmlStreamReader
    By sophister in forum Qt Programming
    Replies: 6
    Last Post: 24th August 2011, 17:31
  2. how QXmlStreamReader parse QString
    By SamSong in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2009, 14:56
  3. QXmlStreamReader issue
    By yagabey in forum Qt Programming
    Replies: 11
    Last Post: 31st December 2008, 21:06
  4. QXmlStreamReader error on first line?
    By warry in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2008, 14:20
  5. QXmlStreamReader rewind
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2008, 08:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.