Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Premature end of document with QXmlStreamReader
QtWS25 Last Chance

Premature end of document with QXmlStreamReader

Scheduled Pinned Locked Moved Solved General and Desktop
xml parsingqxmlstreamreade
3 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaperMoon
    wrote on 1 Dec 2017, 06:05 last edited by
    #1

    Hello,

    Need help please.

    Using QXmlStreamReader, I am trying to parse XML received in QNetworkReply. A simple read to parse XML ends up with error 'Premature end of document'.

    Thanks for any pointers/inputs.

    Qt version 5.9.2
    Linux Debian

    The code that I have to receive and parse XML

    Extract

            QNetworkReply* reply = manager.post(request, query.toUtf8());
    
            QEventLoop eventLoop;
    
            QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
            eventLoop.exec();
    
            if (reply->error() != QNetworkReply::NoError)
            {
                qDebug() << "Network error: " << reply->error();
            }
            else
            {
                qDebug() << "Response XML " << reply->readAll();
    
                QByteArray res = reply->readAll();
    
                parseXML(res);
            }
    
    
    
    void parseXML(QByteArray data)
    {
        QXmlStreamReader xml(data);
    
        while (!xml.atEnd()) 
        {
            if (xml.readNext() != QXmlStreamReader::EndDocument) 
            {
                if (xml.isStartElement())
                    qDebug() << qPrintable(xml.name().toString()) ;
            }
        }
    
        if (xml.hasError())
            qDebug() << "Error: Failed to parse xml " << qPrintable(xml.errorString());
    
    }
    
    
    

    XML received in QNetworkReply

    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
    <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns2=\"http://tempuri.org/ns2.xsd\" xmlns:ns1=\"http://tempuri.org/ns1.xsd\">
     <SOAP-ENV:Body>
      <ns1:getAirfieldList>
       <list>
        <item>
         <ICAO>YSSY</ICAO>
         <description>Sydney Intl</description>
         <runways>
          <item>
           <ends SOAP-ENC:arrayType=\"ns2:RunwayEnd[2]\">
            <item>
             <designator>16R</designator>
             <latitude>-33.929358000000001</latitude>
             <longitude>151.171603</longitude>
             <elevation>0</elevation>
            </item>
            <item>
             <designator>34L</designator>
             <latitude>-33.964275000000001</latitude>
             <longitude>151.18066099999999</longitude>
             <elevation>0</elevation>
            </item>
           </ends>
          </item>
          <item>
           <ends SOAP-ENC:arrayType=\"ns2:RunwayEnd[2]\">
            <item>
             <designator>07</designator>
             <latitude>-33.943750000000001</latitude>
             <longitude>151.163633</longitude>
             <elevation>0</elevation>
            </item>
            <item>
             <designator>25</designator>
             <latitude>-33.937539000000001</latitude>
             <longitude>151.189956</longitude>
             <elevation>0</elevation>
            </item>
           </ends>
          </item>
          <item>
           <ends SOAP-ENC:arrayType=\"ns2:RunwayEnd[2]\">
            <item>
             <designator>16L</designator>
             <latitude>-33.949618999999998</latitude>
             <longitude>151.18831700000001</longitude>
             <elevation>0</elevation>
            </item>
            <item>
             <designator>34R</designator>
             <latitude>-33.971097</latitude>
             <longitude>151.19391100000001</longitude>
             <elevation>0</elevation>
            </item>
           </ends>
          </item>
         </runways>
         <magneticVar>0</magneticVar>
        </item>
       </list>
      </ns1:getAirfieldList>
     </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>\r\n"
    
    J 1 Reply Last reply 1 Dec 2017, 13:20
    0
    • P PaperMoon
      1 Dec 2017, 06:05

      Hello,

      Need help please.

      Using QXmlStreamReader, I am trying to parse XML received in QNetworkReply. A simple read to parse XML ends up with error 'Premature end of document'.

      Thanks for any pointers/inputs.

      Qt version 5.9.2
      Linux Debian

      The code that I have to receive and parse XML

      Extract

              QNetworkReply* reply = manager.post(request, query.toUtf8());
      
              QEventLoop eventLoop;
      
              QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
              eventLoop.exec();
      
              if (reply->error() != QNetworkReply::NoError)
              {
                  qDebug() << "Network error: " << reply->error();
              }
              else
              {
                  qDebug() << "Response XML " << reply->readAll();
      
                  QByteArray res = reply->readAll();
      
                  parseXML(res);
              }
      
      
      
      void parseXML(QByteArray data)
      {
          QXmlStreamReader xml(data);
      
          while (!xml.atEnd()) 
          {
              if (xml.readNext() != QXmlStreamReader::EndDocument) 
              {
                  if (xml.isStartElement())
                      qDebug() << qPrintable(xml.name().toString()) ;
              }
          }
      
          if (xml.hasError())
              qDebug() << "Error: Failed to parse xml " << qPrintable(xml.errorString());
      
      }
      
      
      

      XML received in QNetworkReply

      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
      <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns2=\"http://tempuri.org/ns2.xsd\" xmlns:ns1=\"http://tempuri.org/ns1.xsd\">
       <SOAP-ENV:Body>
        <ns1:getAirfieldList>
         <list>
          <item>
           <ICAO>YSSY</ICAO>
           <description>Sydney Intl</description>
           <runways>
            <item>
             <ends SOAP-ENC:arrayType=\"ns2:RunwayEnd[2]\">
              <item>
               <designator>16R</designator>
               <latitude>-33.929358000000001</latitude>
               <longitude>151.171603</longitude>
               <elevation>0</elevation>
              </item>
              <item>
               <designator>34L</designator>
               <latitude>-33.964275000000001</latitude>
               <longitude>151.18066099999999</longitude>
               <elevation>0</elevation>
              </item>
             </ends>
            </item>
            <item>
             <ends SOAP-ENC:arrayType=\"ns2:RunwayEnd[2]\">
              <item>
               <designator>07</designator>
               <latitude>-33.943750000000001</latitude>
               <longitude>151.163633</longitude>
               <elevation>0</elevation>
              </item>
              <item>
               <designator>25</designator>
               <latitude>-33.937539000000001</latitude>
               <longitude>151.189956</longitude>
               <elevation>0</elevation>
              </item>
             </ends>
            </item>
            <item>
             <ends SOAP-ENC:arrayType=\"ns2:RunwayEnd[2]\">
              <item>
               <designator>16L</designator>
               <latitude>-33.949618999999998</latitude>
               <longitude>151.18831700000001</longitude>
               <elevation>0</elevation>
              </item>
              <item>
               <designator>34R</designator>
               <latitude>-33.971097</latitude>
               <longitude>151.19391100000001</longitude>
               <elevation>0</elevation>
              </item>
             </ends>
            </item>
           </runways>
           <magneticVar>0</magneticVar>
          </item>
         </list>
        </ns1:getAirfieldList>
       </SOAP-ENV:Body>
      </SOAP-ENV:Envelope>\r\n"
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Dec 2017, 13:20 last edited by
      #2

      @PaperMoon First: you need to wait for the reply - you cannot just read after you sent the request. Either use http://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead or readyRead() signal.

      Second: do not call readAll() twice!

      qDebug() << "Response XML " << reply->readAll();
      QByteArray res = reply->readAll(); // Here you will not get anything as you already read everything above!
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 4 Dec 2017, 01:10
      5
      • J jsulm
        1 Dec 2017, 13:20

        @PaperMoon First: you need to wait for the reply - you cannot just read after you sent the request. Either use http://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead or readyRead() signal.

        Second: do not call readAll() twice!

        qDebug() << "Response XML " << reply->readAll();
        QByteArray res = reply->readAll(); // Here you will not get anything as you already read everything above!
        
        P Offline
        P Offline
        PaperMoon
        wrote on 4 Dec 2017, 01:10 last edited by PaperMoon 12 Apr 2017, 01:16
        #3

        @jsulm Thank you so much! I modified the implementation based on your suggestion. And the error was resolved.

        For anyone landing at this question in the forum, Qt examples directory has a lot of examples for similar cases. I found 'RSS Listing example' (Examples/Qt-5.9.2/xml/rsslisting) to be helpful for my scenario.

        1 Reply Last reply
        0

        3/3

        4 Dec 2017, 01:10

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved