Max Frai
Max Frai

Reputation: 64326

XML file from internet and Qt

I want to parse some XML-file in Qt, but that file is located at some server in web. When I used QML I was able to use XMLHttpRequest class which takes address of file in internet (what I do need).

I have only one idea: use network request and download that xml directly. The idea is maybe there is a special interface in XML parser in qt which takes xml-path from internet?

Upvotes: 2

Views: 5776

Answers (3)

Naszta
Naszta

Reputation: 7744

As I know, you should download it. QHttp provides easy way to download it to a temporary file.

QTemporaryFile temp_file;
QHttp http("example.com");
http.get("/your.xml",&temp_file);

New version (based on QNetworkAccessManager):

QNetworkAccessManager * manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileIsReady(QNetworkReply*)) );
manager->get(QNetworkRequest(QUrl("http://example.com/your.xml")));
...
void fileIsReady( QNetworkReply * reply)
{
  QTemporaryFile temp_file;
  temp_file.write(reply->readAll());
}

Upvotes: 10

Frank Osterfeld
Frank Osterfeld

Reputation: 25165

QtXML doesn't do any networking itself. It operates on QIODevices, which is a generic interface for anything doing I/O (files, network sockets, ...). You can either download the XML to temp file and then parse it, or, if you parse incrementally e.g. using QXmlStreamReader, parse the data directly as it arrives:

QNetworkAccessManager *manager = new QNetworkAccessManager( this );
QNetworkReply* reply = QNetworkAccessManager::get( manager->get( QNetworkRequest( QUrl("http://www.foo.com/example.xml") ) ) ;
QXmlStreamReader reader( reply );
//...parse

Note that QXmlStreamReader::PrematureEndOfDocumentErrors can occurr while parsing, if there is not enough data yet downloaded. You can either connect to the reply's readyRead() signal to continue, or use reply->waitForReadyRead() if you're parsing outside of the UI thread.

Upvotes: 1

Adrien Rey-Jarthon
Adrien Rey-Jarthon

Reputation: 1174

I don't know any way to download the file directly from QXML, i think you should download your ressource using QNetwork first and parse it after.

Upvotes: 1

Related Questions