Results 1 to 3 of 3

Thread: Problem with QXmlStreamReader

  1. #1
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with QXmlStreamReader

    Hi!
    I'm having trouble reading the follwing Xml file:

    Qt Code:
    1. <ArrayOfScore xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Score><GameKey>201610629</GameKey><SeasonType>1</SeasonType><Season>2016</Season><Week>6</Week><Date>2016-10-13T20:25:00</Date><AwayTeam>DEN</AwayTeam><HomeTeam>SD</HomeTeam><AwayScore>13</AwayScore><HomeScore>21</HomeScore><Channel i:nil="true"/><PointSpread>3.0</PointSpread><OverUnder>44.5</OverUnder><Quarter>F</Quarter><TimeRemaining i:nil="true"/><Possession i:nil="true"/><Down i:nil="true"/><Distance i:nil="true"/><YardLine i:nil="true"/><YardLineTerritory i:nil="true"/><RedZone i:nil="true"/><AwayScoreQuarter1>0</AwayScoreQuarter1><AwayScoreQuarter2>3</AwayScoreQuarter2><AwayScoreQuarter3>0</AwayScoreQuarter3><AwayScoreQuarter4>10</AwayScoreQuarter4><AwayScoreOvertime>0</AwayScoreOvertime><HomeScoreQuarter1>7</HomeScoreQuarter1><HomeScoreQuarter2>3</HomeScoreQuarter2><HomeScoreQuarter3>9</HomeScoreQuarter3><HomeScoreQuarter4>2</HomeScoreQuarter4><HomeScoreOvertime>0</HomeScoreOvertime><HasStarted>true</HasStarted><IsInProgress>false</IsInProgress><IsOver>true</IsOver><Has1stQuarterStarted>true</Has1stQuarterStarted><Has2ndQuarterStarted>true</Has2ndQuarterStarted><Has3rdQuarterStarted>true</Has3rdQuarterStarted><Has4thQuarterStarted>true</Has4thQuarterStarted><IsOvertime>false</IsOvertime><DownAndDistance i:nil="true"/><QuarterDescription>Final</QuarterDescription><StadiumID>14</StadiumID><LastUpdated>2016-10-17T14:27:48</LastUpdated><GeoLat>32.783188</GeoLat><GeoLong>-117.119439</GeoLong><ForecastTempLow>58</ForecastTempLow><ForecastTempHigh>76</ForecastTempHigh><ForecastDescription>Sunny</ForecastDescription><ForecastWindChill>70</ForecastWindChill><ForecastWindSpeed>14</ForecastWindSpeed><AwayTeamMoneyLine>-160</AwayTeamMoneyLine><HomeTeamMoneyLine>140</HomeTeamMoneyLine><Canceled>false</Canceled><Closed>true</Closed><LastPlay i:nil="true"/><StadiumDetails><StadiumID>14</StadiumID><Name>Qualcomm Stadium</Name><City>San Diego</City><State>CA</State><Country>USA</Country><Capacity>70561</Capacity><PlayingSurface>Grass</PlayingSurface><GeoLat>32.783188</GeoLat><GeoLong>-117.119439</GeoLong></StadiumDetails></Score></ArrayOfScore>
    To copy to clipboard, switch view to plain text mode 

    I use the following code to try to read the elements:

    Qt Code:
    1. QFile testxml("./test.xml");
    2. if(testxml.open(QIODevice::ReadOnly)) {
    3. QXmlStreamReader XMLData(&testxml);
    4. while(!XMLData.atEnd() && !XMLData.hasError()) {
    5. XMLData.readNext();
    6. qDebug() << XMLData.readElementText();
    7. if(XMLData.isStartElement()) {
    8. qDebug() << XMLData.name();
    9. qDebug() << XMLData.readElementText();
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    But I only get two empty strings.

    I have tried reading another Xml file which looks very similar and which is also everything in one line and I can successfully parse the elements. With this file it is somehow not working.

    Does anyone have any idea what could I be doing wrong?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with QXmlStreamReader

    QXmlStreamReader::readElementText() defaults to QXmlStreamReader::ErrorOnUnexpectedElement. Raise an UnexpectedElementError and return what was read so far when a child element is encountered. At line 6 this occurs and prints an empty string. I think this leaves the current element pointing at the end element marker, so the condition at line 7 is not met. The next iteration moves to EOF and line 6 prints another empty string before the while loop condition fails. Single step to verify.

  3. The following user says thank you to ChrisW67 for this useful post:

    ognomir (19th October 2016)

  4. #3
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QXmlStreamReader

    Thanks, it worked! The problem was that all elements are child elements therefore nothing was read before the child elements.

Similar Threads

  1. QXmlStreamReader help?
    By nthung in forum Newbie
    Replies: 2
    Last Post: 4th October 2011, 10:03
  2. QXmlStreamReader
    By sophister in forum Qt Programming
    Replies: 6
    Last Post: 24th August 2011, 18:31
  3. Possilble Bug? QXmlStreamReader
    By dempsey001 in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2010, 19:46
  4. Need help with QXmlStreamReader
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2009, 10:26
  5. QXmlStreamReader problems
    By rishid in forum Newbie
    Replies: 2
    Last Post: 26th June 2008, 12:43

Tags for this Thread

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.