Premature end of document with QXmlStreamReader



  • 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"
    

  • Moderators

    @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!
    


  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.