[Solved]XML downloadfile



  • How can I wait to the file is completely downloaded and then read the file.
    i have tried this code but it doesn't do what I was hopeing for.

    @void MainWindow::test()
    {

    manager = new QNetworkAccessManager(this);
    reply = manager->get(QNetworkRequest(QUrl("http://www.w3schools.com/xml/simple.xml")));
    connect(reply,SIGNAL(finished()),SLOT(downloaded()));
    

    QXmlStreamReader reader( reply);
    int i=0;
    while (!reader.atEnd() && !reader.hasError())
    {
    if (reader.isStartElement())// && xml_stream.name() =="description")
    ui->textBrowser->append( QString::number(reader.lineNumber())
    + ":" + reader.readElementText());

        reader.readNext();
        i++;
    }
    

    qDebug() << i;
    }

    void MainWindow::downloaded()
    {
    QFile file("C:/testprojekt/filesXML/myTest.xml");
    file.open(QIODevice::WriteOnly);
    file.write(reply->readAll());
    file.close();
    reply->waitForReadyRead(1000);
    }@



  • You have to put your parser in the downloaded slot as well if you really want to read the file once it's written.

    Get inspiration from here and [there])http://developer.qt.nokia.com/forums/viewthread/7425)

    I coded something similar to what (I guess) you want to achieve:

    void FileFetcher::setUrl( const QString& url )
    {
      Reply = Mgr -> get( QNetworkRequest( QUrl( url ) ) ); 
      connect( Reply, SIGNAL( finished() ), this, SLOT( downloaded() ) ); 
    }
    
    QIODevice * FileFetcher::networkReply() const
    {
      return Reply;
    }
    
    void FileFetcher::downloaded()
    {
    emit( fileFetched( Reply -> error() );
    }
    

    and I have another class which listens to the fileFetched signal:

    RequestMgr::RequestMgr( /* ... */ )
    {
    /*
    ...
    */
     connect( Fetcher, SIGNAL( fileFetched( QNetworkReply::NetworkError ) ),
        this, SLOT( readText( QNetworkReply::NetworkError ) ) );
    }
    
    void RequestMgr::readText( const QNetworkReply::NetworkError& statut )
    {
      if ( statut != QNetworkReply::NoError )
        {
          cerr << "An error occured while getting the file : "
        << statut << endl;
          return;
        }
      
        cout << "No problem in getting file" << endl;
      
      QIODevice * infos = Fetcher -> networkReply(); //
      
      XmlStreamReader * parser = new XmlStreamReader( infos );
    
    /*
    do whatever you want with the parser
    */
    }
    

    NB : I have a more complex class structure than what I presented here (moreover I had to translate variables and class names), this is not guaranteed to be 100% correct.



  • I'll try this and thank you again

    Works fine


Log in to reply
 

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